<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>风格指南 | Vue.js</title>
    <meta name="generator" content="VuePress 1.6.0">
    <link href="static/css/css.css" rel="stylesheet">
    <link href="static/css/font-awesome.min.css" rel="stylesheet">
    <link rel="icon" href="static/picture/logo.png">
    <link rel="manifest" href="https://v3.cn.vuejs.org/manifest.json">
    <link rel="apple-touch-icon" href="https://v3.cn.vuejs.org/images/icons/apple-icon-152x152.png">
    <script src="static/js/iframeapi.js"></script>
    <meta name="description" content="Vue.js - The 渐进式 JavaScript 框架">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/images/icons/ms-icon-144x144.png">
    <meta name="msapplication-TileColor" content="#000000">
    <link rel="preload" href="static/css/0.styles.e45bfda3.css" as="style"><link rel="preload" href="https://v3.cn.vuejs.org/assets/js/app.824bcde7.js" as="script"><link rel="preload" href="https://v3.cn.vuejs.org/assets/js/11.8d95dd7b.js" as="script"><link rel="preload" href="https://v3.cn.vuejs.org/assets/js/34.1f3cec39.js" as="script"><link rel="preload" href="https://v3.cn.vuejs.org/assets/js/22.4fdda504.js" as="script"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/1.dccf396c.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/100.fc218074.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/101.46997daa.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/102.a8287271.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/103.10b588d8.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/104.fc07a53a.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/105.b651587e.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/106.67863e9b.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/107.e79c6b19.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/108.b26ff980.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/109.118140b0.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/110.e754d95f.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/111.d1708f42.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/112.655a1e60.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/113.39959172.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/114.3743b6be.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/115.1ade1533.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/116.185594b9.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/117.4d956cd5.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/118.3852b43e.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/119.e537a58e.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/12.5f0559f4.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/120.2cf9d5e0.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/121.fd29161b.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/122.25f4761d.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/123.dafaf565.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/124.f2105f04.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/125.1cf41e2b.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/126.b7748dcb.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/127.007667f6.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/128.408bcc87.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/129.11514e0d.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/13.a565cd43.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/130.f9c18e2f.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/131.c4d957d3.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/132.05436c43.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/133.960087d9.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/134.ee4d5043.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/135.2ecd32a0.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/136.3fb78c84.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/137.f38f3d48.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/138.65a9494b.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/139.de20b4d8.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/14.2d21f461.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/140.d3875f31.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/141.ee8f28f9.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/142.437ce12a.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/143.8abbb663.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/144.f5979731.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/145.d5f0fe45.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/146.b6c39a4e.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/147.514b18f4.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/148.a3e8d328.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/149.15baf1d4.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/15.8f8d3b5c.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/150.b9c36787.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/151.3a894ff9.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/152.5766f7d4.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/153.93c1b6de.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/154.0aa4c34b.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/155.74d15feb.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/156.ca5e525d.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/157.e23c89d9.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/158.7dfcabb6.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/159.1b9dfed7.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/16.99589a89.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/160.36c14f30.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/161.8133b8c8.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/162.caa8890c.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/163.ff24f737.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/164.8cb301f0.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/165.b348eba0.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/166.7569a096.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/167.c1738ea7.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/168.b74bb5fc.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/169.beed47bc.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/17.062d3911.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/170.f9694505.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/171.e26727e0.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/172.8cf8bf35.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/173.9f35771c.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/174.588c4f32.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/175.ac782ccb.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/176.94af6e18.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/177.95a59f4e.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/178.2f4b0630.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/179.ae2c0c85.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/18.0d13a77a.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/180.aaf24e43.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/181.c74c3103.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/182.deb7feb5.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/183.f37cb028.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/184.936d716a.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/185.c13461e9.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/186.25a1b30c.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/187.3bc768f9.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/188.b6e719b6.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/19.fe2d40c2.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/2.75ae43f1.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/20.738c9388.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/21.2ae0f44c.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/23.4669b1a1.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/24.17353396.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/25.b7cca482.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/26.ba9e1fec.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/27.46bcb1e9.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/28.fb95e358.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/29.504650ba.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/3.a1647fcd.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/30.a38666bf.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/31.46fb8892.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/32.c952e38f.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/33.323c8512.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/35.e275d2cf.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/36.55488e69.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/37.a0a851d0.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/38.82bb4c68.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/39.3146a2f9.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/4.080ac393.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/40.51bb585e.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/41.b98ed5a7.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/42.49926c98.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/43.6dc26c8f.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/44.8fd7ca2d.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/45.d303ca8a.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/46.9e806617.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/47.d8359429.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/48.a02e830d.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/49.9677f9cb.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/5.234680b7.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/50.0e7ee6af.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/51.d5f9c70d.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/52.cacb0725.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/53.e6479c42.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/54.477106ad.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/55.f782853a.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/56.8a6be712.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/57.54bf3ea7.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/58.a75c3bf9.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/59.49e59cbf.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/6.e1197fdc.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/60.ec1227b4.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/61.4f7ca373.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/62.e32f9a48.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/63.83d7df7f.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/64.d7ff14f5.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/65.668e2939.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/66.2c186f2f.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/67.cc7023fa.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/68.eb21f291.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/69.75c1478e.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/7.f22e56d0.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/70.29cc00e5.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/71.f52bfa61.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/72.5ad0ff13.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/73.fbd2e4a5.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/74.acad0152.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/75.b2864493.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/76.a2f9a23b.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/77.5e7e72a0.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/78.39cadb69.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/79.4bcaef78.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/80.9376feb2.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/81.03c4f395.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/82.bf0047bb.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/83.454e906d.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/84.38bb4dd3.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/85.67183d06.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/86.f7bafa2b.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/87.4877482e.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/88.116856b3.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/89.ca38fd17.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/90.d1a562b0.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/91.86e3a73f.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/92.ad92531c.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/93.4b6eea7a.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/94.31d2b932.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/95.362c65a6.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/96.1b1e79f1.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/97.4ffcf79c.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/98.905c4ddd.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/99.1bed6b0b.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/vendors~docsearch.4746c1d8.js"><link rel="prefetch" href="https://v3.cn.vuejs.org/assets/js/vendors~search-page.a66a448f.js">
    <link rel="stylesheet" href="static/css/0.styles.e45bfda3.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><!----> <header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewbox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="index.html" class="home-link router-link-active"><img src="static/picture/logo.png" alt="Vue.js" class="logo"> <span class="site-name can-hide">Vue.js</span></a> <div class="links"><!----> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文档菜单" class="dropdown-title"><span class="title">文档</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="introduction.html" class="nav-link">
  教程
</a></li><li class="dropdown-item"><!----> <a href="" aria-current="page" class="nav-link router-link-exact-active router-link-active">
  风格指南
</a></li><li class="dropdown-item"><!----> <a href="index2.html" class="nav-link">
  Cookbook
</a></li><li class="dropdown-item"><!----> <a href="markdown.html" class="nav-link">
  示例
</a></li><li class="dropdown-item"><!----> <a href="writing-guide.html" class="nav-link">
  贡献文档
</a></li><li class="dropdown-item"><!----> <a href="introduction1.html" class="nav-link">
  从 Vue 2 迁移
</a></li></ul></div></div><div class="nav-item"><a href="index3.html" class="nav-link">
  API 参考
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="生态系统" class="dropdown-title"><span class="title">生态系统</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          社区
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="team.html" class="nav-link">
  团队
</a></li><li class="dropdown-subitem"><a href="partners.html" class="nav-link">
  合作伙伴
</a></li><li class="dropdown-subitem"><a href="join.html" class="nav-link">
  加入
</a></li><li class="dropdown-subitem"><a href="themes.html" class="nav-link">
  主题
</a></li></ul></li><li class="dropdown-item"><h4>
          官方项目
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="index4.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Router
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="index5.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vuex
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="index6.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue CLI
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="index7.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Test Utils
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="index8.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Devtools
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="index9.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Weekly news
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="index10.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Blog
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="支持 Vue" class="dropdown-title"><span class="title">支持 Vue</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="index11.html#一次性捐款" class="nav-link">
  一次性捐款
</a></li><li class="dropdown-item"><!----> <a href="index11.html#周期性赞助" class="nav-link">
  周期性捐款
</a></li><li class="dropdown-item"><!----> <a href="javascript:;" target="_blank" rel="noopener noreferrer" class="nav-link external">
  贴纸
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="javascript:;" target="_blank" rel="noopener noreferrer" class="nav-link external">
  周边
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="javascript:;" target="_blank" rel="noopener noreferrer" class="nav-link external">
  T-Shirt 商店
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="多语言" class="dropdown-title"><span class="title">多语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="index12.html" target="_blank" class="nav-link external">
  English
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="index13.html" target="_blank" class="nav-link external">
  한국어
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="index14.html" target="_blank" class="nav-link external">
  日本語
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="index15.html" target="_blank" class="nav-link external">
  Русский
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="translations.html#community-translations" class="nav-link">
  更多翻译
</a></li></ul></div></div> <a href="javascript:;" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav> <div id="docsearch"></div></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文档菜单" class="dropdown-title"><span class="title">文档</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="introduction.html" class="nav-link">
  教程
</a></li><li class="dropdown-item"><!----> <a href="" aria-current="page" class="nav-link router-link-exact-active router-link-active">
  风格指南
</a></li><li class="dropdown-item"><!----> <a href="index2.html" class="nav-link">
  Cookbook
</a></li><li class="dropdown-item"><!----> <a href="markdown.html" class="nav-link">
  示例
</a></li><li class="dropdown-item"><!----> <a href="writing-guide.html" class="nav-link">
  贡献文档
</a></li><li class="dropdown-item"><!----> <a href="introduction1.html" class="nav-link">
  从 Vue 2 迁移
</a></li></ul></div></div><div class="nav-item"><a href="index3.html" class="nav-link">
  API 参考
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="生态系统" class="dropdown-title"><span class="title">生态系统</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          社区
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="team.html" class="nav-link">
  团队
</a></li><li class="dropdown-subitem"><a href="partners.html" class="nav-link">
  合作伙伴
</a></li><li class="dropdown-subitem"><a href="join.html" class="nav-link">
  加入
</a></li><li class="dropdown-subitem"><a href="themes.html" class="nav-link">
  主题
</a></li></ul></li><li class="dropdown-item"><h4>
          官方项目
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="index4.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Router
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="index5.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vuex
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="index6.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue CLI
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="index7.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Vue Test Utils
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="index8.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Devtools
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="index9.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Weekly news
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-subitem"><a href="index10.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Blog
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="支持 Vue" class="dropdown-title"><span class="title">支持 Vue</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="index11.html#一次性捐款" class="nav-link">
  一次性捐款
</a></li><li class="dropdown-item"><!----> <a href="index11.html#周期性赞助" class="nav-link">
  周期性捐款
</a></li><li class="dropdown-item"><!----> <a href="javascript:;" target="_blank" rel="noopener noreferrer" class="nav-link external">
  贴纸
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="javascript:;" target="_blank" rel="noopener noreferrer" class="nav-link external">
  周边
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="javascript:;" target="_blank" rel="noopener noreferrer" class="nav-link external">
  T-Shirt 商店
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="多语言" class="dropdown-title"><span class="title">多语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="index12.html" target="_blank" class="nav-link external">
  English
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="index13.html" target="_blank" class="nav-link external">
  한국어
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="index14.html" target="_blank" class="nav-link external">
  日本語
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="index15.html" target="_blank" class="nav-link external">
  Русский
  <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="translations.html#community-translations" class="nav-link">
  更多翻译
</a></li></ul></div></div> <a href="javascript:;" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>风格指南</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="#规则类别" class="sidebar-link">规则类别</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="#优先级-a-必要的" class="sidebar-link">优先级 A：必要的</a></li><li class="sidebar-sub-header"><a href="#优先级-b-强烈推荐" class="sidebar-link">优先级 B：强烈推荐</a></li><li class="sidebar-sub-header"><a href="#优先级-c-推荐" class="sidebar-link">优先级 C：推荐</a></li><li class="sidebar-sub-header"><a href="#优先级-d-谨慎使用" class="sidebar-link">优先级 D：谨慎使用</a></li></ul></li><li><a href="#优先级-a-的规则-必要的-规避错误" class="sidebar-link">优先级 A 的规则：必要的</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="#组件名为多个单词必要" class="sidebar-link">组件名为多个单词</a></li><li class="sidebar-sub-header"><a href="#prop-定义必要" class="sidebar-link">Prop 定义</a></li><li class="sidebar-sub-header"><a href="#为-v-for-设置-key-值必要" class="sidebar-link">为 v-for 设置 key 值</a></li><li class="sidebar-sub-header"><a href="#避免-v-if-和-v-for-一起使用必要" class="sidebar-link">避免 v-if 和 v-for 一起使用</a></li><li class="sidebar-sub-header"><a href="#为组件样式设置作用域必要" class="sidebar-link">为组件样式设置作用域</a></li><li class="sidebar-sub-header"><a href="#私有-property-名称必要" class="sidebar-link">私有 property 名称</a></li></ul></li><li><a href="#优先级-b-的规则-强烈推荐-增强代码可读性" class="sidebar-link">优先级 B 的规则：强烈推荐</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="#组件文件强烈推荐" class="sidebar-link">组件文件</a></li><li class="sidebar-sub-header"><a href="#单文件组件文件的大小写强烈推荐" class="sidebar-link">单文件组件文件的大小写</a></li><li class="sidebar-sub-header"><a href="#基础组件名称强烈推荐" class="sidebar-link">基础组件名称</a></li><li class="sidebar-sub-header"><a href="#单例组件名称强烈推荐" class="sidebar-link">单例组件名称</a></li><li class="sidebar-sub-header"><a href="#紧密耦合的组件名称强烈推荐" class="sidebar-link">紧密耦合的组件名称</a></li><li class="sidebar-sub-header"><a href="#组件名称中的单词顺序强烈推荐" class="sidebar-link">组件名称中的单词顺序</a></li><li class="sidebar-sub-header"><a href="#自闭合组件强烈推荐" class="sidebar-link">自闭合组件</a></li><li class="sidebar-sub-header"><a href="#模板中的组件名称大小写强烈推荐" class="sidebar-link">模板中的组件名称大小写</a></li><li class="sidebar-sub-header"><a href="#js-jsx-中使用的组件名称强烈推荐" class="sidebar-link">JS/JSX 中使用的组件名称</a></li><li class="sidebar-sub-header"><a href="#完整单词的组件名称强烈推荐" class="sidebar-link">完整单词的组件名称</a></li><li class="sidebar-sub-header"><a href="#prop-命名强烈推荐" class="sidebar-link">Prop 命名</a></li><li class="sidebar-sub-header"><a href="#多个-attribute-的元素强烈推荐" class="sidebar-link">多个 attribute 的元素</a></li><li class="sidebar-sub-header"><a href="#模板中的简单表达式强烈推荐" class="sidebar-link">模板中的简单表达式</a></li><li class="sidebar-sub-header"><a href="#简单的计算属性强烈推荐" class="sidebar-link">简单的计算属性</a></li><li class="sidebar-sub-header"><a href="#带引号的-attribute-值强烈推荐" class="sidebar-link">带引号的 attribute 值</a></li><li class="sidebar-sub-header"><a href="#指令缩写强烈推荐" class="sidebar-link">指令缩写</a></li></ul></li><li><a href="#优先级-c-的规则-推荐-将选择和认知成本最小化" class="sidebar-link">优先级 C 的规则：推荐</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="#组件-实例选项的顺序推荐" class="sidebar-link">组件/实例选项的顺序</a></li><li class="sidebar-sub-header"><a href="#元素-attribute-的顺序推荐" class="sidebar-link">元素 attribute 的顺序</a></li><li class="sidebar-sub-header"><a href="#组件-实例选项中的空行推荐" class="sidebar-link">组件/实例选项中的空行</a></li><li class="sidebar-sub-header"><a href="#单文件组件的顶级元素的顺序推荐" class="sidebar-link">单文件组件的顶级元素的顺序</a></li></ul></li><li><a href="#优先级-d-的规则-谨慎使用-潜在风险" class="sidebar-link">优先级 D 的规则：谨慎使用</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="#scoped-中的元素选择器谨慎使用" class="sidebar-link">scoped 中的元素选择器</a></li><li class="sidebar-sub-header"><a href="#隐性的父子组件通信谨慎使用" class="sidebar-link">隐性的父子组件通信</a></li><li class="sidebar-sub-header"><a href="#非-flux-的全局状态管理谨慎使用" class="sidebar-link">非 Flux 的全局状态管理</a></li></ul></li></ul></section></li></ul> </aside> <main class="page"><div class="carbon-ads"></div>  <div class="theme-default-content content__default" data-v-179547fc="" data-v-179547fc=""><h1 id="风格指南" data-v-179547fc=""><a href="#风格指南" class="header-anchor" data-v-179547fc="">#</a> 风格指南</h1> <p data-v-179547fc="">本文是针对 Vue 代码的官方风格指南。如果你在工程中使用了 Vue，本文可以帮助你回避错误和反模式，减少对细枝末节的过度纠结。不过，我们认为任何风格指南都不会完美适用于所有的团队或项目。因此，我们也建议你根据过去的经验、周边的技术栈或个人价值观来做出有意识的调整。</p> <p data-v-179547fc="">对于其中的绝大部分，我们也总体上避免就 JavaScript 或 HTML 本身提出建议。我们不介意你是否使用分号或尾逗号，也不介意你在 HTML attribute 中使用单引号还是双引号。不过，当我们发现某个特定模式在 Vue 语境中很有用时，也会存在例外。</p> <p data-v-179547fc="">最终，我们把规则归为了四个大类：</p> <h2 id="规则类别" data-v-179547fc=""><a href="#规则类别" class="header-anchor" data-v-179547fc="">#</a> 规则类别</h2> <h3 id="优先级-a-必要的" data-v-179547fc=""><a href="#优先级-a-必要的" class="header-anchor" data-v-179547fc="">#</a> 优先级 A：必要的</h3> <p data-v-179547fc="">这些规则会帮你规避错误，所以请务必学习并遵守它们。可能存在例外的情况，但应该非常少，而且需要同时精通 JavaScript 和 Vue 的人来决定。</p> <h3 id="优先级-b-强烈推荐" data-v-179547fc=""><a href="#优先级-b-强烈推荐" class="header-anchor" data-v-179547fc="">#</a> 优先级 B：强烈推荐</h3> <p data-v-179547fc="">这些规则能够在绝大多数工程中改善可读性和开发体验。即使你违反了它们，代码还是能照常运行。但此类例外应该尽可能地少，且有充分的理由。</p> <h3 id="优先级-c-推荐" data-v-179547fc=""><a href="#优先级-c-推荐" class="header-anchor" data-v-179547fc="">#</a> 优先级 C：推荐</h3> <p data-v-179547fc="">当存在有多个同样好的选项时，可以选择其中任意一项以确保一致性。在这些规则里，我们描述每个可取的选项，并建议使用一个默认的。也就是说，只要保持一致且理由充分，你可以自由地在代码库中做出不同的选择。不过，请务必给出一个好的理由！通过接受社区的标准，你将会：</p> <ol data-v-179547fc=""><li data-v-179547fc="">训练你的大脑，以便更容易的处理你在社区遇到的代码；</li> <li data-v-179547fc="">不做修改就可以直接复制粘贴社区的代码示例；</li> <li data-v-179547fc="">能够经常招聘到和你编码习惯相同的新人，至少在 Vue 的领域。</li></ol> <h3 id="优先级-d-谨慎使用" data-v-179547fc=""><a href="#优先级-d-谨慎使用" class="header-anchor" data-v-179547fc="">#</a> 优先级 D：谨慎使用</h3> <p data-v-179547fc="">某些 Vue 特性的存在是为了照顾极端情况，或帮助老代码平稳迁移。当被过度使用时，这些特性会让代码难以维护，甚至变成 bug 的来源。这些规则的存在是为了给有潜在风险的特性敲响警钟，说明它们在哪些时候应该被避免使用，以及为什么。</p> <h2 id="优先级-a-的规则-必要的-规避错误" data-v-179547fc=""><a href="#优先级-a-的规则-必要的-规避错误" class="header-anchor" data-v-179547fc="">#</a> 优先级 A 的规则：必要的 <span class="hide-from-sidebar" data-v-179547fc="">(规避错误)</span></h2> <h3 id="组件名为多个单词必要" data-v-179547fc=""><a href="#组件名为多个单词必要" class="header-anchor" data-v-179547fc="">#</a> 组件名为多个单词<sup data-p="a" data-v-179547fc="">必要</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">组件名应该始终由多个单词组成，除了根组件 <code data-v-179547fc="">App</code>，以及 <code data-v-179547fc="">&lt;transition&gt;</code>、<code data-v-179547fc="">&lt;component&gt;</code> 之类的 Vue 内置组件。</strong></p> <p data-v-179547fc="">这样做可以避免与现有以及未来的 HTML 元素<a href="javascript:;" target="_blank" rel="noopener noreferrer" data-v-179547fc="">产生冲突<svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound" data-v-179547fc=""><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>，因为所有的 HTML 元素名称都是单个单词的。</p> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc="">app<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">component</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">'todo'</span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
<span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">)</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token keyword" data-v-179547fc="">export</span> <span class="token keyword" data-v-179547fc="">default</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">name</span><span class="token operator" data-v-179547fc="">:</span> <span class="token string" data-v-179547fc="">'Todo'</span><span class="token punctuation" data-v-179547fc="">,</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc="">app<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">component</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">'todo-item'</span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
<span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">)</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token keyword" data-v-179547fc="">export</span> <span class="token keyword" data-v-179547fc="">default</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">name</span><span class="token operator" data-v-179547fc="">:</span> <span class="token string" data-v-179547fc="">'TodoItem'</span><span class="token punctuation" data-v-179547fc="">,</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div></div> <h3 id="prop-定义必要" data-v-179547fc=""><a href="#prop-定义必要" class="header-anchor" data-v-179547fc="">#</a> Prop 定义<sup data-p="a" data-v-179547fc="">必要</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">Prop 定义应尽量详细</strong></p> <p data-v-179547fc="">在提交的代码中，prop 的定义应该尽量详细，至少指定其类型。</p> <details class="custom-block details" data-v-179547fc=""><summary data-v-179547fc="">详解</summary> <p data-v-179547fc="">细致的 <a href="component-props.html#prop-验证" data-v-179547fc="">prop 定义</a>有两个优势：</p> <ul data-v-179547fc=""><li data-v-179547fc="">它们写明了组件的 API，所以组件的设计用法可以通俗易懂；</li> <li data-v-179547fc="">在开发环境下，如果为一个组件提供了格式不正确的 prop，Vue 将会告警，以帮助你捕获潜在的错误来源。</li></ul></details> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">// 只有在原型开发时，这么做才能被接受</span>
<span class="token literal-property property" data-v-179547fc="">props</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">[</span><span class="token string" data-v-179547fc="">'status'</span><span class="token punctuation" data-v-179547fc="">]</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token literal-property property" data-v-179547fc="">props</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">status</span><span class="token operator" data-v-179547fc="">:</span> String
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">// 更好的例子</span>
<span class="token literal-property property" data-v-179547fc="">props</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">status</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token literal-property property" data-v-179547fc="">type</span><span class="token operator" data-v-179547fc="">:</span> String<span class="token punctuation" data-v-179547fc="">,</span>
    <span class="token literal-property property" data-v-179547fc="">required</span><span class="token operator" data-v-179547fc="">:</span> <span class="token boolean" data-v-179547fc="">true</span><span class="token punctuation" data-v-179547fc="">,</span>

    <span class="token function-variable function" data-v-179547fc="">validator</span><span class="token operator" data-v-179547fc="">:</span> <span class="token parameter" data-v-179547fc="">value</span> <span class="token operator" data-v-179547fc="">=&gt;</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token keyword" data-v-179547fc="">return</span> <span class="token punctuation" data-v-179547fc="">[</span>
        <span class="token string" data-v-179547fc="">'syncing'</span><span class="token punctuation" data-v-179547fc="">,</span>
        <span class="token string" data-v-179547fc="">'synced'</span><span class="token punctuation" data-v-179547fc="">,</span>
        <span class="token string" data-v-179547fc="">'version-conflict'</span><span class="token punctuation" data-v-179547fc="">,</span>
        <span class="token string" data-v-179547fc="">'error'</span>
      <span class="token punctuation" data-v-179547fc="">]</span><span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">includes</span><span class="token punctuation" data-v-179547fc="">(</span>value<span class="token punctuation" data-v-179547fc="">)</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">12</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">13</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">14</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">15</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">16</span><br data-v-179547fc=""></div></div></div> <h3 id="为-v-for-设置-key-值必要" data-v-179547fc=""><a href="#为-v-for-设置-key-值必要" class="header-anchor" data-v-179547fc="">#</a> 为 <code data-v-179547fc="">v-for</code> 设置 key 值<sup data-p="a" data-v-179547fc="">必要</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">始终以 <code data-v-179547fc="">key</code> 配合 <code data-v-179547fc="">v-for</code>。</strong></p> <p data-v-179547fc="">在组件上必须始终以 <code data-v-179547fc="">key</code> 配合 <code data-v-179547fc="">v-for</code>，以便维护内部组件及其子树的状态。即使对于元素，维持可预测的行为也是一种好的做法。例如动画中的<a href="javascript:;" target="_blank" rel="noopener noreferrer" data-v-179547fc="">对象固化 (object constancy)<svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound" data-v-179547fc=""><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> 。</p> <details class="custom-block details" data-v-179547fc=""><summary data-v-179547fc="">详解</summary> <p data-v-179547fc="">假设你有一个待办事项列表：</p> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token function" data-v-179547fc="">data</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token keyword" data-v-179547fc="">return</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token literal-property property" data-v-179547fc="">todos</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">[</span>
      <span class="token punctuation" data-v-179547fc="">{</span>
        <span class="token literal-property property" data-v-179547fc="">id</span><span class="token operator" data-v-179547fc="">:</span> <span class="token number" data-v-179547fc="">1</span><span class="token punctuation" data-v-179547fc="">,</span>
        <span class="token literal-property property" data-v-179547fc="">text</span><span class="token operator" data-v-179547fc="">:</span> <span class="token string" data-v-179547fc="">'Learn to use v-for'</span>
      <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>
      <span class="token punctuation" data-v-179547fc="">{</span>
        <span class="token literal-property property" data-v-179547fc="">id</span><span class="token operator" data-v-179547fc="">:</span> <span class="token number" data-v-179547fc="">2</span><span class="token punctuation" data-v-179547fc="">,</span>
        <span class="token literal-property property" data-v-179547fc="">text</span><span class="token operator" data-v-179547fc="">:</span> <span class="token string" data-v-179547fc="">'Learn to use key'</span>
      <span class="token punctuation" data-v-179547fc="">}</span>
    <span class="token punctuation" data-v-179547fc="">]</span>
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">12</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">13</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">14</span><br data-v-179547fc=""></div></div><p data-v-179547fc="">然后你把它们按照字母顺序排序。在更新 DOM 的时候，Vue 将会优化渲染，尽可能地将 DOM 操作的代价降到最低。言下之意，可能是删掉第一个待办事项元素，然后把它重新加回到列表的最末尾。</p> <p data-v-179547fc="">问题在于，可能会存在某种情况，不删除将要保留在 DOM 中的元素也很重要。比如说，你想使用 <code data-v-179547fc="">&lt;transition-group&gt;</code> 给列表排序增加动画，或在被渲染元素是 <code data-v-179547fc="">&lt;input&gt;</code> 时保留焦点状态。在这些情况下，为每一个项目添加一个唯一的键值 (比如 <code data-v-179547fc="">:key=&quot;todo.id&quot;</code>) 将会让 Vue 知道如何使行为更容易预测。</p> <p data-v-179547fc="">根据我们的经验，最好<em data-v-179547fc="">始终</em>添加一个唯一的键值，这样你和你的团队永远不必担心这些极端情况。在极少数对性能有严格要求的情况下，为了避免对象固化，你可以刻意做一些非常规的处理。</p></details> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>li</span> <span class="token attr-name" data-v-179547fc="">v-for</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>todo in todos<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
    {{ todo.text }}
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>li</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>li</span>
    <span class="token attr-name" data-v-179547fc="">v-for</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>todo in todos<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
    <span class="token attr-name" data-v-179547fc="">:key</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>todo.id<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
  <span class="token punctuation" data-v-179547fc="">&gt;</span></span>
    {{ todo.text }}
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>li</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""></div></div></div> <h3 id="避免-v-if-和-v-for-一起使用必要" data-v-179547fc=""><a href="#避免-v-if-和-v-for-一起使用必要" class="header-anchor" data-v-179547fc="">#</a> 避免 <code data-v-179547fc="">v-if</code> 和 <code data-v-179547fc="">v-for</code> 一起使用<sup data-p="a" data-v-179547fc="">必要</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">永远不要在一个元素上同时使用 <code data-v-179547fc="">v-if</code> 和 <code data-v-179547fc="">v-for</code>。</strong></p> <p data-v-179547fc="">一般我们在两种常见的情况下会倾向于这样做：</p> <ul data-v-179547fc=""><li data-v-179547fc=""><p data-v-179547fc="">为了对列表中的项目进行过滤 (比如 <code data-v-179547fc="">v-for=&quot;user in users&quot; v-if=&quot;user.isActive&quot;</code>)。在这种情形下，请将 <code data-v-179547fc="">users</code> 替换为一个计算属性 (比如 <code data-v-179547fc="">activeUsers</code>)，返回过滤后的列表。</p></li> <li data-v-179547fc=""><p data-v-179547fc="">为了避免渲染本应该被隐藏的列表 (比如 <code data-v-179547fc="">v-for=&quot;user in users&quot; v-if=&quot;shouldShowUsers&quot;</code>)。这种情形下，请将 <code data-v-179547fc="">v-if</code> 移动至容器元素上 (比如 <code data-v-179547fc="">ul</code>、<code data-v-179547fc="">ol</code>)。</p></li></ul> <details class="custom-block details" data-v-179547fc=""><summary data-v-179547fc="">详解</summary> <p data-v-179547fc="">当 Vue 处理指令时，<code data-v-179547fc="">v-if</code> 比 <code data-v-179547fc="">v-for</code> 具有更高的优先级，所以这个模板：</p> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>li</span>
    <span class="token attr-name" data-v-179547fc="">v-for</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user in users<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
    <span class="token attr-name" data-v-179547fc="">v-if</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user.isActive<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
    <span class="token attr-name" data-v-179547fc="">:key</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user.id<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
  <span class="token punctuation" data-v-179547fc="">&gt;</span></span>
    {{ user.name }}
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>li</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""></div></div><p data-v-179547fc="">将抛出一个错误，因为 <code data-v-179547fc="">v-if</code> 指令将首先被执行，而迭代的变量 <code data-v-179547fc="">user</code> 此时还不存在。</p> <p data-v-179547fc="">这可以通过遍历一个计算属性来解决，像这样：</p> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token literal-property property" data-v-179547fc="">computed</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token function" data-v-179547fc="">activeUsers</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token keyword" data-v-179547fc="">return</span> <span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>users<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">filter</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token parameter" data-v-179547fc="">user</span> <span class="token operator" data-v-179547fc="">=&gt;</span> user<span class="token punctuation" data-v-179547fc="">.</span>isActive<span class="token punctuation" data-v-179547fc="">)</span>
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>li</span>
    <span class="token attr-name" data-v-179547fc="">v-for</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user in activeUsers<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
    <span class="token attr-name" data-v-179547fc="">:key</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user.id<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
  <span class="token punctuation" data-v-179547fc="">&gt;</span></span>
    {{ user.name }}
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>li</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""></div></div><p data-v-179547fc="">另外，我们也可以使用 <code data-v-179547fc="">&lt;template&gt;</code> 标签和 <code data-v-179547fc="">v-for</code> 来包裹 <code data-v-179547fc="">&lt;li&gt;</code> 元素。</p> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span> <span class="token attr-name" data-v-179547fc="">v-for</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user in users<span class="token punctuation" data-v-179547fc="">&quot;</span></span> <span class="token attr-name" data-v-179547fc="">:key</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user.id<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
    <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>li</span> <span class="token attr-name" data-v-179547fc="">v-if</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user.isActive<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
      {{ user.name }}
    <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>li</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""></div></div></details> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>li</span>
    <span class="token attr-name" data-v-179547fc="">v-for</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user in users<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
    <span class="token attr-name" data-v-179547fc="">v-if</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user.isActive<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
    <span class="token attr-name" data-v-179547fc="">:key</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user.id<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
  <span class="token punctuation" data-v-179547fc="">&gt;</span></span>
    {{ user.name }}
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>li</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>li</span>
    <span class="token attr-name" data-v-179547fc="">v-for</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user in activeUsers<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
    <span class="token attr-name" data-v-179547fc="">:key</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user.id<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
  <span class="token punctuation" data-v-179547fc="">&gt;</span></span>
    {{ user.name }}
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>li</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span> <span class="token attr-name" data-v-179547fc="">v-for</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user in users<span class="token punctuation" data-v-179547fc="">&quot;</span></span> <span class="token attr-name" data-v-179547fc="">:key</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user.id<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
    <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>li</span> <span class="token attr-name" data-v-179547fc="">v-if</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>user.isActive<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
      {{ user.name }}
    <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>li</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>ul</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""></div></div></div> <h3 id="为组件样式设置作用域必要" data-v-179547fc=""><a href="#为组件样式设置作用域必要" class="header-anchor" data-v-179547fc="">#</a> 为组件样式设置作用域<sup data-p="a" data-v-179547fc="">必要</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">对于应用来说，样式在顶层 <code data-v-179547fc="">App</code> 组件和布局组件中可以是全局的，但是在其它所有组件中都应该是有作用域的。</strong></p> <p data-v-179547fc="">这条规则只适用于<a href="single-file-component.html" data-v-179547fc="">单文件组件</a>。你<em data-v-179547fc="">不一定</em>要使用 <a href="scoped-css.html" target="_blank" rel="noopener noreferrer" data-v-179547fc=""><code data-v-179547fc="">scoped</code> attribute<svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound" data-v-179547fc=""><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>。作用域也可以通过 <a href="css-modules.html" target="_blank" rel="noopener noreferrer" data-v-179547fc="">CSS Modules<svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound" data-v-179547fc=""><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> (一个基于 class 的，类似 <a href="javascript:;" target="_blank" rel="noopener noreferrer" data-v-179547fc="">BEM<svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound" data-v-179547fc=""><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> 的策略) 或者其它的库/约定来实现。</p> <p data-v-179547fc=""><strong data-v-179547fc="">不管怎样，对于组件库来说，我们应该更倾向于选用基于 class 的策略，而不是 <code data-v-179547fc="">scoped</code> attribute。</strong></p> <p data-v-179547fc="">这会让覆写内部样式变得更容易：使用人类可理解的 class 名称，没有太高的选择器优先级，而且不太会导致冲突。</p> <details class="custom-block details" data-v-179547fc=""><summary data-v-179547fc="">详解</summary> <p data-v-179547fc="">如果你正在和其他开发者一起开发一个大型工程，或有时候要引入三方 HTML/CSS (比如来自 Auth0)，设置一致的作用域能确保你的样式只会运用在它们想要作用的组件上。</p> <p data-v-179547fc="">除了 <code data-v-179547fc="">scoped</code> attribute 以外，使用唯一的 class 名可以帮你确保那些第三方库的 CSS 不会运用在你自己的 HTML 上。比如说，许多工程都使用了 <code data-v-179547fc="">button</code>、<code data-v-179547fc="">btn</code> 或 <code data-v-179547fc="">icon</code> class 名，所以即便你不使用类似 BEM 的策略，添加一个 app 或组件专属的前缀 (比如 <code data-v-179547fc="">ButtonClose-icon</code>) 也可以提供一些保护。</p></details> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>button</span> <span class="token attr-name" data-v-179547fc="">class</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>btn btn-close<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>×<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>button</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>

<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token style" data-v-179547fc=""><span class="token language-css" data-v-179547fc="">
<span class="token selector" data-v-179547fc="">.btn-close</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token property" data-v-179547fc="">background-color</span><span class="token punctuation" data-v-179547fc="">:</span> red<span class="token punctuation" data-v-179547fc="">;</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>button</span> <span class="token attr-name" data-v-179547fc="">class</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>button button-close<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>×<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>button</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>

<span class="token comment" data-v-179547fc="">&lt;!-- 使用 `scoped` attribute --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>style</span> <span class="token attr-name" data-v-179547fc="">scoped</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token style" data-v-179547fc=""><span class="token language-css" data-v-179547fc="">
<span class="token selector" data-v-179547fc="">.button</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token property" data-v-179547fc="">border</span><span class="token punctuation" data-v-179547fc="">:</span> none<span class="token punctuation" data-v-179547fc="">;</span>
  <span class="token property" data-v-179547fc="">border-radius</span><span class="token punctuation" data-v-179547fc="">:</span> 2px<span class="token punctuation" data-v-179547fc="">;</span>
<span class="token punctuation" data-v-179547fc="">}</span>

<span class="token selector" data-v-179547fc="">.button-close</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token property" data-v-179547fc="">background-color</span><span class="token punctuation" data-v-179547fc="">:</span> red<span class="token punctuation" data-v-179547fc="">;</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">12</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">13</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">14</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">15</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>button</span> <span class="token attr-name" data-v-179547fc="">:class</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>[$style.button, $style.buttonClose]<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>×<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>button</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>

<span class="token comment" data-v-179547fc="">&lt;!-- 使用 CSS modules --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>style</span> <span class="token attr-name" data-v-179547fc="">module</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token style" data-v-179547fc=""><span class="token language-css" data-v-179547fc="">
<span class="token selector" data-v-179547fc="">.button</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token property" data-v-179547fc="">border</span><span class="token punctuation" data-v-179547fc="">:</span> none<span class="token punctuation" data-v-179547fc="">;</span>
  <span class="token property" data-v-179547fc="">border-radius</span><span class="token punctuation" data-v-179547fc="">:</span> 2px<span class="token punctuation" data-v-179547fc="">;</span>
<span class="token punctuation" data-v-179547fc="">}</span>

<span class="token selector" data-v-179547fc="">.buttonClose</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token property" data-v-179547fc="">background-color</span><span class="token punctuation" data-v-179547fc="">:</span> red<span class="token punctuation" data-v-179547fc="">;</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">12</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">13</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">14</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">15</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>button</span> <span class="token attr-name" data-v-179547fc="">class</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>c-Button c-Button--close<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>×<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>button</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>

<span class="token comment" data-v-179547fc="">&lt;!-- 使用 BEM 约定 --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token style" data-v-179547fc=""><span class="token language-css" data-v-179547fc="">
<span class="token selector" data-v-179547fc="">.c-Button</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token property" data-v-179547fc="">border</span><span class="token punctuation" data-v-179547fc="">:</span> none<span class="token punctuation" data-v-179547fc="">;</span>
  <span class="token property" data-v-179547fc="">border-radius</span><span class="token punctuation" data-v-179547fc="">:</span> 2px<span class="token punctuation" data-v-179547fc="">;</span>
<span class="token punctuation" data-v-179547fc="">}</span>

<span class="token selector" data-v-179547fc="">.c-Button--close</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token property" data-v-179547fc="">background-color</span><span class="token punctuation" data-v-179547fc="">:</span> red<span class="token punctuation" data-v-179547fc="">;</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">12</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">13</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">14</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">15</span><br data-v-179547fc=""></div></div></div> <h3 id="私有-property-名称必要" data-v-179547fc=""><a href="#私有-property-名称必要" class="header-anchor" data-v-179547fc="">#</a> 私有 property 名称<sup data-p="a" data-v-179547fc="">必要</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">使用模块作用域来确保外部无法访问到私有函数。如果无法做到这一点，就始终应该为插件、mixin 等不考虑对外公开 API 的自定义私有 property 使用 <code data-v-179547fc="">$_</code> 前缀。并附带一个命名空间，以回避和其它作者的冲突 (比如 <code data-v-179547fc="">$_yourPluginName_</code>)。</strong></p> <details class="custom-block details" data-v-179547fc=""><summary data-v-179547fc="">详解</summary> <p data-v-179547fc="">Vue 使用 <code data-v-179547fc="">_</code> 前缀来定义其自身的私有 property，所以使用相同的前缀 (比如 <code data-v-179547fc="">_update</code>) 有覆写实例 property 的风险。即便你检查确认了 Vue 当前版本没有用到这个 property 名，也不能保证和将来的版本没有冲突。</p> <p data-v-179547fc="">对于 <code data-v-179547fc="">$</code> 前缀来说，其在 Vue 生态系统中的目的是暴露给用户的一个特殊的实例 property，所以把它用于<em data-v-179547fc="">私有</em> property 并不合适。</p> <p data-v-179547fc="">不过，我们推荐把这两个前缀结合为 <code data-v-179547fc="">$_</code>，作为一个用户定义的私有 property 的约定，以确保不会和 Vue 自身相冲突。</p></details> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token keyword" data-v-179547fc="">const</span> myGreatMixin <span class="token operator" data-v-179547fc="">=</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
  <span class="token literal-property property" data-v-179547fc="">methods</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token function" data-v-179547fc="">update</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token comment" data-v-179547fc="">// ...</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token keyword" data-v-179547fc="">const</span> myGreatMixin <span class="token operator" data-v-179547fc="">=</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
  <span class="token literal-property property" data-v-179547fc="">methods</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token function" data-v-179547fc="">_update</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token comment" data-v-179547fc="">// ...</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token keyword" data-v-179547fc="">const</span> myGreatMixin <span class="token operator" data-v-179547fc="">=</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
  <span class="token literal-property property" data-v-179547fc="">methods</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token function" data-v-179547fc="">$update</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token comment" data-v-179547fc="">// ...</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token keyword" data-v-179547fc="">const</span> myGreatMixin <span class="token operator" data-v-179547fc="">=</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
  <span class="token literal-property property" data-v-179547fc="">methods</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token function" data-v-179547fc="">$_update</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token comment" data-v-179547fc="">// ...</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token keyword" data-v-179547fc="">const</span> myGreatMixin <span class="token operator" data-v-179547fc="">=</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
  <span class="token literal-property property" data-v-179547fc="">methods</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token function" data-v-179547fc="">$_myGreatMixin_update</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token comment" data-v-179547fc="">// ...</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">// Even better!</span>
<span class="token keyword" data-v-179547fc="">const</span> myGreatMixin <span class="token operator" data-v-179547fc="">=</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
  <span class="token literal-property property" data-v-179547fc="">methods</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token function" data-v-179547fc="">publicMethod</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token comment" data-v-179547fc="">// ...</span>
      <span class="token function" data-v-179547fc="">myPrivateFunction</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span>

<span class="token keyword" data-v-179547fc="">function</span> <span class="token function" data-v-179547fc="">myPrivateFunction</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
<span class="token punctuation" data-v-179547fc="">}</span>

<span class="token keyword" data-v-179547fc="">export</span> <span class="token keyword" data-v-179547fc="">default</span> myGreatMixin
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">12</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">13</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">14</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">15</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">16</span><br data-v-179547fc=""></div></div></div> <h2 id="优先级-b-的规则-强烈推荐-增强代码可读性" data-v-179547fc=""><a href="#优先级-b-的规则-强烈推荐-增强代码可读性" class="header-anchor" data-v-179547fc="">#</a> 优先级 B 的规则：强烈推荐 <span class="hide-from-sidebar" data-v-179547fc="">(增强代码可读性)</span></h2> <h3 id="组件文件强烈推荐" data-v-179547fc=""><a href="#组件文件强烈推荐" class="header-anchor" data-v-179547fc="">#</a> 组件文件<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">只要有能够拼接文件的构建系统，就把每个组件单独分成文件。</strong></p> <p data-v-179547fc="">当你需要编辑一个组件，或查阅一个组件的用法时，这种做法可以帮助你更快速地找到它。</p> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc="">app<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">component</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">'TodoList'</span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
<span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">)</span>

app<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">component</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">'TodoItem'</span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
<span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">)</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- TodoList.js
|- TodoItem.js
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div><div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- TodoList.vue
|- TodoItem.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div></div> <h3 id="单文件组件文件的大小写强烈推荐" data-v-179547fc=""><a href="#单文件组件文件的大小写强烈推荐" class="header-anchor" data-v-179547fc="">#</a> 单文件组件文件的大小写<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc=""><a href="single-file-component.html" data-v-179547fc="">单文件组件</a>的文件名应该要么始终是单词大写开头 (PascalCase)，要么始终是横线连接 (kebab-case)。</strong></p> <p data-v-179547fc="">单词大写开头对于代码编辑器的自动补全最为友好，因为这使得我们在 JS(X) 和模板中引用组件的方式尽可能地一致。然而，混用大小写的文件命名方式，有时候会导致其在大小写不敏感的文件系统中出现问题，这也是横线连接命名同样完全可取的原因。</p> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- mycomponent.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div><div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- myComponent.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- MyComponent.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div><div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- my-component.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div></div> <h3 id="基础组件名称强烈推荐" data-v-179547fc=""><a href="#基础组件名称强烈推荐" class="header-anchor" data-v-179547fc="">#</a> 基础组件名称<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">应用特定样式和约定的基础组件 (也就是展示类的、无逻辑的或无状态的组件) 应该全部以一个特定的前缀开头，比如 <code data-v-179547fc="">Base</code>、<code data-v-179547fc="">App</code> 或 <code data-v-179547fc="">V</code>。</strong></p> <details class="custom-block details" data-v-179547fc=""><summary data-v-179547fc="">详解</summary> <p data-v-179547fc="">这些组件为你的应用奠定了一致的基础样式和行为。它们可能<strong data-v-179547fc="">只</strong>包括：</p> <ul data-v-179547fc=""><li data-v-179547fc="">HTML 元素</li> <li data-v-179547fc="">其它基础组件</li> <li data-v-179547fc="">第三方 UI 组件</li></ul> <p data-v-179547fc="">但是它们<strong data-v-179547fc="">绝不会</strong>包括全局状态 (比如来自 Vuex store)。</p> <p data-v-179547fc="">它们的名字通常包含所包裹元素的名字 (比如 <code data-v-179547fc="">BaseButton</code>、<code data-v-179547fc="">BaseTable</code>)，除非没有现成的对应功能的元素 (比如 <code data-v-179547fc="">BaseIcon</code>)。如果你为特定的上下文构建类似的组件，那它们几乎总会消费这些组件 (比如 <code data-v-179547fc="">BaseButton</code> 可能会用在 <code data-v-179547fc="">ButtonSubmit</code> 上)。</p> <p data-v-179547fc="">这样做的几个好处：</p> <ul data-v-179547fc=""><li data-v-179547fc=""><p data-v-179547fc="">当你在编辑器中以字母顺序排序时，你的应用的基础组件会全部列在一起，这样更容易识别。</p></li> <li data-v-179547fc=""><p data-v-179547fc="">因为组件名应该始终是多个单词，所以这样做可以避免你在包裹简单组件时随意选择前缀 (比如 <code data-v-179547fc="">MyButton</code>、<code data-v-179547fc="">VueButton</code>)。</p></li> <li data-v-179547fc=""><p data-v-179547fc="">因为这些组件会被频繁使用，所以你可能想要把它们注册到全局，而不是在各处分别导入它们。结合一个前缀即可使用 webpack 达到目的：</p> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token keyword" data-v-179547fc="">const</span> requireComponent <span class="token operator" data-v-179547fc="">=</span> require<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">context</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">&quot;./src&quot;</span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token boolean" data-v-179547fc="">true</span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token regex" data-v-179547fc=""><span class="token regex-delimiter" data-v-179547fc="">/</span><span class="token regex-source language-regex" data-v-179547fc="">Base[A-Z]\w+\.(vue|js)$</span><span class="token regex-delimiter" data-v-179547fc="">/</span></span><span class="token punctuation" data-v-179547fc="">)</span>
requireComponent<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">keys</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span><span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">forEach</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token keyword" data-v-179547fc="">function</span> <span class="token punctuation" data-v-179547fc="">(</span><span class="token parameter" data-v-179547fc="">fileName</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token keyword" data-v-179547fc="">let</span> baseComponentConfig <span class="token operator" data-v-179547fc="">=</span> <span class="token function" data-v-179547fc="">requireComponent</span><span class="token punctuation" data-v-179547fc="">(</span>fileName<span class="token punctuation" data-v-179547fc="">)</span>
  baseComponentConfig <span class="token operator" data-v-179547fc="">=</span> baseComponentConfig<span class="token punctuation" data-v-179547fc="">.</span>default <span class="token operator" data-v-179547fc="">||</span> baseComponentConfig
  <span class="token keyword" data-v-179547fc="">const</span> baseComponentName <span class="token operator" data-v-179547fc="">=</span> baseComponentConfig<span class="token punctuation" data-v-179547fc="">.</span>name <span class="token operator" data-v-179547fc="">||</span> <span class="token punctuation" data-v-179547fc="">(</span>
    fileName
      <span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">replace</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token regex" data-v-179547fc=""><span class="token regex-delimiter" data-v-179547fc="">/</span><span class="token regex-source language-regex" data-v-179547fc="">^.+\/</span><span class="token regex-delimiter" data-v-179547fc="">/</span></span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token string" data-v-179547fc="">''</span><span class="token punctuation" data-v-179547fc="">)</span>
      <span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">replace</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token regex" data-v-179547fc=""><span class="token regex-delimiter" data-v-179547fc="">/</span><span class="token regex-source language-regex" data-v-179547fc="">\.\w+$</span><span class="token regex-delimiter" data-v-179547fc="">/</span></span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token string" data-v-179547fc="">''</span><span class="token punctuation" data-v-179547fc="">)</span>
  <span class="token punctuation" data-v-179547fc="">)</span>
  app<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">component</span><span class="token punctuation" data-v-179547fc="">(</span>baseComponentName<span class="token punctuation" data-v-179547fc="">,</span> baseComponentConfig<span class="token punctuation" data-v-179547fc="">)</span>
<span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">)</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""></div></div></li></ul></details> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- MyButton.vue
|- VueTable.vue
|- Icon.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- BaseButton.vue
|- BaseTable.vue
|- BaseIcon.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div><div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- AppButton.vue
|- AppTable.vue
|- AppIcon.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div><div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- VButton.vue
|- VTable.vue
|- VIcon.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div></div> <h3 id="单例组件名称强烈推荐" data-v-179547fc=""><a href="#单例组件名称强烈推荐" class="header-anchor" data-v-179547fc="">#</a> 单例组件名称<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">只应该拥有单个活跃实例的组件应该以 <code data-v-179547fc="">The</code> 前缀命名，以示其唯一性。</strong></p> <p data-v-179547fc="">这并不意味着组件只可被用于一个页面，而是<em data-v-179547fc="">每个页面</em>只能使用一次。这些组件永远不接受任何 prop，因为它们是为你的应用所定制的，而不是它们所在的上下文。如果你发现有必要添加 prop，那就表明这实际上是一个可复用的组件，只不过<em data-v-179547fc="">目前</em>在每个页面里只使用一次。</p> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- Heading.vue
|- MySidebar.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- TheHeading.vue
|- TheSidebar.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div></div> <h3 id="紧密耦合的组件名称强烈推荐" data-v-179547fc=""><a href="#紧密耦合的组件名称强烈推荐" class="header-anchor" data-v-179547fc="">#</a> 紧密耦合的组件名称<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">与父组件紧密耦合的子组件应该以父组件名作为前缀命名。</strong></p> <p data-v-179547fc="">如果一个组件只在某个特定父组件的上下文中有意义，那么这层关系应该体现在其命名上。因为编辑器通常会按字母顺序组织文件，这么做也可以把相关联的文件排放在一起。</p> <details class="custom-block details" data-v-179547fc=""><summary data-v-179547fc="">详解</summary> <p data-v-179547fc="">你也许会尝试通过在其父组件命名的目录中嵌套子组件以解决这个问题。比如：</p> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- TodoList/
   |- Item/
      |- index.vue
      |- Button.vue
   |- index.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""></div></div><p data-v-179547fc="">或:</p> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- TodoList/
   |- Item/
      |- Button.vue
   |- Item.vue
|- TodoList.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""></div></div><p data-v-179547fc="">但是这种方式并不推荐，因为这会导致：</p> <ul data-v-179547fc=""><li data-v-179547fc="">许多文件的名字相同，使得在编辑器中快速切换文件变得困难。</li> <li data-v-179547fc="">过多嵌套的子目录增加了在编辑器侧边栏中浏览组件所花费的时间。</li></ul></details> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- TodoList.vue
|- TodoItem.vue
|- TodoButton.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div><div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- SearchSidebar.vue
|- NavigationForSearchSidebar.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- TodoList.vue
|- TodoListItem.vue
|- TodoListItemButton.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div><div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- SearchSidebar.vue
|- SearchSidebarNavigation.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div></div> <h3 id="组件名称中的单词顺序强烈推荐" data-v-179547fc=""><a href="#组件名称中的单词顺序强烈推荐" class="header-anchor" data-v-179547fc="">#</a> 组件名称中的单词顺序<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">组件名称应该以高阶的 (通常是一般化描述的) 单词开头，并以描述性的修饰词结尾。</strong></p> <details class="custom-block details" data-v-179547fc=""><summary data-v-179547fc="">详解</summary> <p data-v-179547fc="">你可能会疑惑：</p> <blockquote data-v-179547fc=""><p data-v-179547fc="">“为什么我们在给组件命名时不多遵从自然语言呢？”</p></blockquote> <p data-v-179547fc="">在自然的英文里，形容词和其它描述语通常都出现在名词之前，否则就需要用到连接词。比如：</p> <ul data-v-179547fc=""><li data-v-179547fc="">Coffee <em data-v-179547fc="">with</em> milk</li> <li data-v-179547fc="">Soup <em data-v-179547fc="">of the</em> day</li> <li data-v-179547fc="">Visitor <em data-v-179547fc="">to the</em> museum</li></ul> <p data-v-179547fc="">如果你愿意，你完全可以在组件名里包含这些连接词，但是单词的顺序仍然很重要。</p> <p data-v-179547fc="">同样要注意<strong data-v-179547fc="">在你的应用中所谓的“高阶”是跟语境有关的</strong>。比如对于一个带搜索表单的应用来说，它可能包含这样的组件：</p> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- ClearSearchButton.vue
|- ExcludeFromSearchInput.vue
|- LaunchOnStartupCheckbox.vue
|- RunSearchButton.vue
|- SearchInput.vue
|- TermsCheckbox.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""></div></div><p data-v-179547fc="">你可能已经注意到了，我们很难看出来哪些组件是针对搜索的。现在我们来根据规则给组件重新命名：</p> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- SearchButtonClear.vue
|- SearchButtonRun.vue
|- SearchInputExcludeGlob.vue
|- SearchInputQuery.vue
|- SettingsCheckboxLaunchOnStartup.vue
|- SettingsCheckboxTerms.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""></div></div><p data-v-179547fc="">因为编辑器通常会按字母顺序组织文件，所以现在组件之间的重要关系一目了然。</p> <p data-v-179547fc="">你也许会想要换种方式解决这个问题，把所有的搜索组件放到“search”目录，把所有的设置组件放到“settings”目录。我们只推荐在非常大型的应用 (如有 100+ 个组件) 下才考虑这么做，因为：</p> <ul data-v-179547fc=""><li data-v-179547fc="">在多级目录间找来找去，通常来说要比在单个 <code data-v-179547fc="">components</code> 目录下滚动查找要花费更多的精力。</li> <li data-v-179547fc="">存在组件重名 (比如存在多个 <code data-v-179547fc="">ButtonDelete.vue</code> 组件) 的时候在编辑器里更难快速定位。</li> <li data-v-179547fc="">重构将变得更困难，因为为一个移动了的组件更新相关引用时，查找/替换通常并不高效。</li></ul></details> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- ClearSearchButton.vue
|- ExcludeFromSearchInput.vue
|- LaunchOnStartupCheckbox.vue
|- RunSearchButton.vue
|- SearchInput.vue
|- TermsCheckbox.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- SearchButtonClear.vue
|- SearchButtonRun.vue
|- SearchInputQuery.vue
|- SearchInputExcludeGlob.vue
|- SettingsCheckboxTerms.vue
|- SettingsCheckboxLaunchOnStartup.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""></div></div></div> <h3 id="自闭合组件强烈推荐" data-v-179547fc=""><a href="#自闭合组件强烈推荐" class="header-anchor" data-v-179547fc="">#</a> 自闭合组件<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">在<a href="single-file-component.html" data-v-179547fc="">单文件组件</a>、字符串模板和 <a href="render-function.html#jsx" data-v-179547fc="">JSX</a> 中，没有内容的组件应该是自闭合的——但在 DOM 模板里永远不要这样做。</strong></p> <p data-v-179547fc="">自闭合组件表示它们不仅没有内容，而且是<strong data-v-179547fc="">刻意</strong>没有内容。其不同之处就好比书中的一页白纸，与贴有“本页有意留白”标签的白纸。而且没有了额外的闭合标签，你的代码也将更简洁。</p> <p data-v-179547fc="">不幸的是，HTML 并不支持自闭合的自定义元素——<a href="javascript:;" target="_blank" rel="noopener noreferrer" data-v-179547fc="">官方的“空”元素<svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound" data-v-179547fc=""><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>除外。所以上述策略仅适用于在进入 DOM 之前，Vue 的模板编译器能够触达的地方，然后再产出符合 DOM 规范的 HTML。</p> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">&lt;!-- 在单文件组件、字符串模板和 JSX 中 --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>MyComponent</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>MyComponent</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">&lt;!-- 在 DOM 模板中                   --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>my-component</span><span class="token punctuation" data-v-179547fc="">/&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">&lt;!-- 在单文件组件、字符串模板和 JSX 中 --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>MyComponent</span><span class="token punctuation" data-v-179547fc="">/&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">&lt;!-- 在 DOM 模板中                   --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>my-component</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>my-component</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div></div> <h3 id="模板中的组件名称大小写强烈推荐" data-v-179547fc=""><a href="#模板中的组件名称大小写强烈推荐" class="header-anchor" data-v-179547fc="">#</a> 模板中的组件名称大小写<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">对于绝大多数项目来说，在<a href="single-file-component.html" data-v-179547fc="">单文件组件</a>和字符串模板中，组件名称应该始终是 PascalCase 的——但是在 DOM 模板中是 kebab-case 的。</strong></p> <p data-v-179547fc="">PascalCase 相比 kebab-case 有一些优势：</p> <ul data-v-179547fc=""><li data-v-179547fc="">编辑器可以在模板里自动补全组件名称，因为 PascalCase 同样适用于 JavaScript。</li> <li data-v-179547fc=""><code data-v-179547fc="">&lt;MyComponent&gt;</code> 视觉上比 <code data-v-179547fc="">&lt;my-component&gt;</code> 更能够和单个单词的 HTML 元素区别开来，因为前者的不同之处有两个大写字母，后者只有一个横线。</li> <li data-v-179547fc="">如果你在模板中使用任何非 Vue 的自定义元素，比如一个 Web Component，PascalCase 确保了你的 Vue 组件在视觉上仍然是易识别的。</li></ul> <p data-v-179547fc="">不幸的是，由于 HTML 是大小写不敏感的，在 DOM 模板中必须仍使用 kebab-case。</p> <p data-v-179547fc="">还请注意，如果你已经是 kebab-case 的重度用户，那么与 HTML 保持一致的命名约定，且在多个项目中保持相同的大小写规则，就可能比上述优势更为重要了。在这些情况下，<strong data-v-179547fc="">在所有的地方都使用 kebab-case 同样是可以接受的</strong>。</p> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">&lt;!-- 在单文件组件和字符串模板中 --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>mycomponent</span><span class="token punctuation" data-v-179547fc="">/&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">&lt;!-- 在单文件组件和字符串模板中 --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>myComponent</span><span class="token punctuation" data-v-179547fc="">/&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">&lt;!-- 在 DOM 模板中            --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>MyComponent</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>MyComponent</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">&lt;!-- 在单文件组件和字符串模板中 --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>MyComponent</span><span class="token punctuation" data-v-179547fc="">/&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">&lt;!-- 在 DOM 模板中            --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>my-component</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>my-component</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div><p data-v-179547fc="">或者</p> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">&lt;!-- 在所有地方 --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>my-component</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>my-component</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div></div> <h3 id="js-jsx-中使用的组件名称强烈推荐" data-v-179547fc=""><a href="#js-jsx-中使用的组件名称强烈推荐" class="header-anchor" data-v-179547fc="">#</a> JS/JSX 中使用的组件名称<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">JS/<a href="render-function.html#jsx" data-v-179547fc="">JSX</a> 中的组件名应该始终是 PascalCase 的，尽管在较为简单的应用中，只使用 <code data-v-179547fc="">app.component</code> 进行全局组件注册时，可以使用 kebab-case 字符串。</strong></p> <details class="custom-block details" data-v-179547fc=""><summary data-v-179547fc="">详解</summary> <p data-v-179547fc="">在 JavaScript 中，PascalCase 是类和构造函数 (本质上来说，任何可以产生多份不同实例的东西) 的命名约定。Vue 组件也有实例，因此同样适用 PascalCase。由此带来的额外好处是，在 JSX (和模板) 里使用 PascalCase 使得代码的读者更容易分辨组件与 HTML 元素。</p> <p data-v-179547fc="">然而，对于<strong data-v-179547fc="">只</strong>通过 <code data-v-179547fc="">app.component</code> 定义全局组件的应用来说，我们推荐 kebab-case 作为替代。原因是：</p> <ul data-v-179547fc=""><li data-v-179547fc="">全局组件很少被 JavaScript 引用，所以遵守 JavaScript 的命名约定意义不大。</li> <li data-v-179547fc="">这些应用往往包含许多 DOM 内的模板，这种情况下是<a href="#%E6%A8%A1%E6%9D%BF%E4%B8%AD%E7%9A%84%E7%BB%84%E4%BB%B6%E5%90%8D%E7%A7%B0%E5%A4%A7%E5%B0%8F%E5%86%99%E5%BC%BA%E7%83%88%E6%8E%A8%E8%8D%90" data-v-179547fc=""><strong data-v-179547fc="">必须</strong>使用 kebab-case</a> 的。</li></ul></details> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc="">app<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">component</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">'myComponent'</span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
<span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">)</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token keyword" data-v-179547fc="">import</span> myComponent <span class="token keyword" data-v-179547fc="">from</span> <span class="token string" data-v-179547fc="">'./MyComponent.vue'</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token keyword" data-v-179547fc="">export</span> <span class="token keyword" data-v-179547fc="">default</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">name</span><span class="token operator" data-v-179547fc="">:</span> <span class="token string" data-v-179547fc="">'myComponent'</span><span class="token punctuation" data-v-179547fc="">,</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token keyword" data-v-179547fc="">export</span> <span class="token keyword" data-v-179547fc="">default</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">name</span><span class="token operator" data-v-179547fc="">:</span> <span class="token string" data-v-179547fc="">'my-component'</span><span class="token punctuation" data-v-179547fc="">,</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc="">app<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">component</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">'MyComponent'</span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
<span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">)</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc="">app<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">component</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">'my-component'</span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
<span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">)</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token keyword" data-v-179547fc="">import</span> MyComponent <span class="token keyword" data-v-179547fc="">from</span> <span class="token string" data-v-179547fc="">'./MyComponent.vue'</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token keyword" data-v-179547fc="">export</span> <span class="token keyword" data-v-179547fc="">default</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">name</span><span class="token operator" data-v-179547fc="">:</span> <span class="token string" data-v-179547fc="">'MyComponent'</span><span class="token punctuation" data-v-179547fc="">,</span>
  <span class="token comment" data-v-179547fc="">// ...</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div></div> <h3 id="完整单词的组件名称强烈推荐" data-v-179547fc=""><a href="#完整单词的组件名称强烈推荐" class="header-anchor" data-v-179547fc="">#</a> 完整单词的组件名称<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">组件名称应该倾向于完整的单词，而不是缩写。</strong></p> <p data-v-179547fc="">编辑器中的自动补全已经让书写长命名的代价非常之低了，而其带来的明确性却是非常宝贵的。不常用的缩写尤其应该避免。</p> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- SdSettings.vue
|- UProfOpts.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language- line-numbers-mode" data-v-179547fc=""><pre class="language-text" data-v-179547fc=""><code data-v-179547fc="">components/
|- StudentDashboardSettings.vue
|- UserProfileOptions.vue
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div></div> <h3 id="prop-命名强烈推荐" data-v-179547fc=""><a href="#prop-命名强烈推荐" class="header-anchor" data-v-179547fc="">#</a> Prop 命名<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">在声明 prop 的时候，其命名应该始终使用 camelCase，而在模板和 <a href="render-function.html#jsx" data-v-179547fc="">JSX</a> 中应该始终使用 kebab-case。</strong></p> <p data-v-179547fc="">我们只是单纯地遵循了每种语言的约定。在 JavaScript 中 camelCase 更为自然。而在 HTML 中则是 kebab-case。</p> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token literal-property property" data-v-179547fc="">props</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token string-property property" data-v-179547fc="">'greeting-text'</span><span class="token operator" data-v-179547fc="">:</span> String
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>WelcomeMessage</span> <span class="token attr-name" data-v-179547fc="">greetingText</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>hi<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">/&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token literal-property property" data-v-179547fc="">props</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">greetingText</span><span class="token operator" data-v-179547fc="">:</span> String
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>WelcomeMessage</span> <span class="token attr-name" data-v-179547fc="">greeting-text</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>hi<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">/&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""></div></div></div> <h3 id="多个-attribute-的元素强烈推荐" data-v-179547fc=""><a href="#多个-attribute-的元素强烈推荐" class="header-anchor" data-v-179547fc="">#</a> 多个 attribute 的元素<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">多个 attribute 的元素应该分多行撰写，每个 attribute 一行。</strong></p> <p data-v-179547fc="">在 JavaScript 中，用多行分隔对象的多个 property 是很常见的最佳实践，因为这样更易读。模板和 <a href="render-function.html#jsx" data-v-179547fc="">JSX</a> 都需要做同样的考量。</p> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>img</span> <span class="token attr-name" data-v-179547fc="">src</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>https://vuejs.org/images/logo.png<span class="token punctuation" data-v-179547fc="">&quot;</span></span> <span class="token attr-name" data-v-179547fc="">alt</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>Vue Logo<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>MyComponent</span> <span class="token attr-name" data-v-179547fc="">foo</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>a<span class="token punctuation" data-v-179547fc="">&quot;</span></span> <span class="token attr-name" data-v-179547fc="">bar</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>b<span class="token punctuation" data-v-179547fc="">&quot;</span></span> <span class="token attr-name" data-v-179547fc="">baz</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>c<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">/&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>img</span>
  <span class="token attr-name" data-v-179547fc="">src</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>https://vuejs.org/images/logo.png<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
  <span class="token attr-name" data-v-179547fc="">alt</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>Vue Logo<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
<span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>MyComponent</span>
  <span class="token attr-name" data-v-179547fc="">foo</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>a<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
  <span class="token attr-name" data-v-179547fc="">bar</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>b<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
  <span class="token attr-name" data-v-179547fc="">baz</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>c<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
<span class="token punctuation" data-v-179547fc="">/&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""></div></div></div> <h3 id="模板中的简单表达式强烈推荐" data-v-179547fc=""><a href="#模板中的简单表达式强烈推荐" class="header-anchor" data-v-179547fc="">#</a> 模板中的简单表达式<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">组件模板应该只包含简单的表达式，复杂的表达式则应该重构为计算属性或方法。</strong></p> <p data-v-179547fc="">复杂表达式会让你的模板变得不那么声明式。我们应该尽量描述应该显示<em data-v-179547fc="">什么</em>，而非<em data-v-179547fc="">如何</em>计算那个值。而且计算属性和方法使得代码可以复用。</p> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc="">{{
  fullName.split(' ').map((word) =&gt; {
    return word[0].toUpperCase() + word.slice(1)
  }).join(' ')
}}
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">&lt;!-- 在模板中 --&gt;</span>
{{ normalizedFullName }}
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">// 复杂表达式已经转为了一个计算属性</span>
<span class="token literal-property property" data-v-179547fc="">computed</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token function" data-v-179547fc="">normalizedFullName</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token keyword" data-v-179547fc="">return</span> <span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>fullName<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">split</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">' '</span><span class="token punctuation" data-v-179547fc="">)</span>
      <span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">map</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token parameter" data-v-179547fc="">word</span> <span class="token operator" data-v-179547fc="">=&gt;</span> word<span class="token punctuation" data-v-179547fc="">[</span><span class="token number" data-v-179547fc="">0</span><span class="token punctuation" data-v-179547fc="">]</span><span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">toUpperCase</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token operator" data-v-179547fc="">+</span> word<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">slice</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token number" data-v-179547fc="">1</span><span class="token punctuation" data-v-179547fc="">)</span><span class="token punctuation" data-v-179547fc="">)</span>
      <span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">join</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">' '</span><span class="token punctuation" data-v-179547fc="">)</span>
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""></div></div></div> <h3 id="简单的计算属性强烈推荐" data-v-179547fc=""><a href="#简单的计算属性强烈推荐" class="header-anchor" data-v-179547fc="">#</a> 简单的计算属性<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">应该把复杂计算属性尽可能多地分割为更简单的计算属性。</strong></p> <details class="custom-block details" data-v-179547fc=""><summary data-v-179547fc="">详解</summary> <p data-v-179547fc="">更简单的、命名得当的计算属性：</p> <ul data-v-179547fc=""><li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">更易于测试</strong></p> <p data-v-179547fc="">当每个计算属性都仅包含一个非常简单，且依赖很少的表达式时，撰写测试以确保其能够正确地工作就会变得容易许多。</p></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">更易于阅读</strong></p> <p data-v-179547fc="">简化计算属性要求你为每一个值都起一个描述性的名称，即便它并没有被复用。这使得其他开发者 (以及未来的你) 更容易专注于他们所关心的代码，并了解现状。</p></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">更好地“拥抱变化”</strong></p> <p data-v-179547fc="">任何能够被命名的值，都有可能被用在视图上。举个例子，我们可能打算展示一个信息，告诉用户他们节省了多少钱。也可能打算计算税费，但是也许会分开展现，而不是作为总价的一部分。</p> <p data-v-179547fc="">小且专注的计算属性对信息将如何被使用所作出的假设更少，因此当需求变更时，重构的工作量也将更小。</p></li></ul></details> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token literal-property property" data-v-179547fc="">computed</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token function" data-v-179547fc="">price</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token keyword" data-v-179547fc="">const</span> basePrice <span class="token operator" data-v-179547fc="">=</span> <span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>manufactureCost <span class="token operator" data-v-179547fc="">/</span> <span class="token punctuation" data-v-179547fc="">(</span><span class="token number" data-v-179547fc="">1</span> <span class="token operator" data-v-179547fc="">-</span> <span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>profitMargin<span class="token punctuation" data-v-179547fc="">)</span>
    <span class="token keyword" data-v-179547fc="">return</span> <span class="token punctuation" data-v-179547fc="">(</span>
      basePrice <span class="token operator" data-v-179547fc="">-</span>
      basePrice <span class="token operator" data-v-179547fc="">*</span> <span class="token punctuation" data-v-179547fc="">(</span><span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>discountPercent <span class="token operator" data-v-179547fc="">||</span> <span class="token number" data-v-179547fc="">0</span><span class="token punctuation" data-v-179547fc="">)</span>
    <span class="token punctuation" data-v-179547fc="">)</span>
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token literal-property property" data-v-179547fc="">computed</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token function" data-v-179547fc="">basePrice</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token keyword" data-v-179547fc="">return</span> <span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>manufactureCost <span class="token operator" data-v-179547fc="">/</span> <span class="token punctuation" data-v-179547fc="">(</span><span class="token number" data-v-179547fc="">1</span> <span class="token operator" data-v-179547fc="">-</span> <span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>profitMargin<span class="token punctuation" data-v-179547fc="">)</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token function" data-v-179547fc="">discount</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token keyword" data-v-179547fc="">return</span> <span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>basePrice <span class="token operator" data-v-179547fc="">*</span> <span class="token punctuation" data-v-179547fc="">(</span><span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>discountPercent <span class="token operator" data-v-179547fc="">||</span> <span class="token number" data-v-179547fc="">0</span><span class="token punctuation" data-v-179547fc="">)</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token function" data-v-179547fc="">finalPrice</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token keyword" data-v-179547fc="">return</span> <span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>basePrice <span class="token operator" data-v-179547fc="">-</span> <span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>discount
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">12</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">13</span><br data-v-179547fc=""></div></div></div> <h3 id="带引号的-attribute-值强烈推荐" data-v-179547fc=""><a href="#带引号的-attribute-值强烈推荐" class="header-anchor" data-v-179547fc="">#</a> 带引号的 attribute 值<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">非空 HTML attribute 的值应该始终带有引号 (单引号或双引号，选择未在 JS 里面使用的那个)。</strong></p> <p data-v-179547fc="">虽然在 HTML 中不带空格的 attribute 的值是可以不加引号的，但这样做往往导致大家<em data-v-179547fc="">避开</em>空格，从而使得 attribute 的可读性变差。</p> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>input</span> <span class="token attr-name" data-v-179547fc="">type</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span>text</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc="">&lt;AppSidebar :style={width:sidebarWidth+'px'}&gt;
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>input</span> <span class="token attr-name" data-v-179547fc="">type</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>text<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>AppSidebar</span> <span class="token attr-name" data-v-179547fc="">:style</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>{ width: sidebarWidth + <span class="token punctuation" data-v-179547fc="">'</span>px<span class="token punctuation" data-v-179547fc="">'</span> }<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""></div></div></div> <h3 id="指令缩写强烈推荐" data-v-179547fc=""><a href="#指令缩写强烈推荐" class="header-anchor" data-v-179547fc="">#</a> 指令缩写<sup data-p="b" data-v-179547fc="">强烈推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">指令缩写 (用 <code data-v-179547fc="">:</code> 表示 <code data-v-179547fc="">v-bind:</code>，<code data-v-179547fc="">@</code> 表示 <code data-v-179547fc="">v-on:</code> 和用 <code data-v-179547fc="">#</code> 表示 <code data-v-179547fc="">v-slot</code>) 应该要么始终使用，要么始终不使用。</strong></p> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>input</span>
  <span class="token attr-name" data-v-179547fc=""><span class="token namespace" data-v-179547fc="">v-bind:</span>value</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>newTodoText<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
  <span class="token attr-name" data-v-179547fc="">:placeholder</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>newTodoInstructions<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
<span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>input</span>
  <span class="token attr-name" data-v-179547fc=""><span class="token namespace" data-v-179547fc="">v-on:</span>input</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>onInput<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
  <span class="token attr-name" data-v-179547fc="">@focus</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>onFocus<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
<span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span> <span class="token attr-name" data-v-179547fc=""><span class="token namespace" data-v-179547fc="">v-slot:</span>header</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>h1</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>Here might be a page title<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>h1</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>

<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span> <span class="token attr-name" data-v-179547fc="">#footer</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>p</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>Here's some contact info<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>p</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>input</span>
  <span class="token attr-name" data-v-179547fc="">:value</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>newTodoText<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
  <span class="token attr-name" data-v-179547fc="">:placeholder</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>newTodoInstructions<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
<span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>input</span>
  <span class="token attr-name" data-v-179547fc=""><span class="token namespace" data-v-179547fc="">v-bind:</span>value</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>newTodoText<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
  <span class="token attr-name" data-v-179547fc=""><span class="token namespace" data-v-179547fc="">v-bind:</span>placeholder</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>newTodoInstructions<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
<span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>input</span>
  <span class="token attr-name" data-v-179547fc="">@input</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>onInput<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
  <span class="token attr-name" data-v-179547fc="">@focus</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>onFocus<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
<span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>input</span>
  <span class="token attr-name" data-v-179547fc=""><span class="token namespace" data-v-179547fc="">v-on:</span>input</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>onInput<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
  <span class="token attr-name" data-v-179547fc=""><span class="token namespace" data-v-179547fc="">v-on:</span>focus</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>onFocus<span class="token punctuation" data-v-179547fc="">&quot;</span></span>
<span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span> <span class="token attr-name" data-v-179547fc=""><span class="token namespace" data-v-179547fc="">v-slot:</span>header</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>h1</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>Here might be a page title<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>h1</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>

<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span> <span class="token attr-name" data-v-179547fc=""><span class="token namespace" data-v-179547fc="">v-slot:</span>footer</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>p</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>Here's some contact info<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>p</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span> <span class="token attr-name" data-v-179547fc="">#header</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>h1</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>Here might be a page title<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>h1</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>

<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span> <span class="token attr-name" data-v-179547fc="">#footer</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>p</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>Here's some contact info<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>p</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""></div></div></div> <h2 id="优先级-c-的规则-推荐-将选择和认知成本最小化" data-v-179547fc=""><a href="#优先级-c-的规则-推荐-将选择和认知成本最小化" class="header-anchor" data-v-179547fc="">#</a> 优先级 C 的规则：推荐 <span class="hide-from-sidebar" data-v-179547fc="">(将选择和认知成本最小化)</span></h2> <h3 id="组件-实例选项的顺序推荐" data-v-179547fc=""><a href="#组件-实例选项的顺序推荐" class="header-anchor" data-v-179547fc="">#</a> 组件/实例选项的顺序<sup data-p="c" data-v-179547fc="">推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">组件/实例的选项应该有统一的顺序。</strong></p> <p data-v-179547fc="">这是我们为组件选项推荐的默认顺序。它们被划分为几大类，你能够由此知道新的 property 应该被放到哪里。</p> <ol data-v-179547fc=""><li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">全局感知</strong> (要求在组件以外被感知)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">name</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">模板编译选项</strong> (改变模板编译的方式)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">compilerOptions</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">模板依赖</strong> (模板内使用的资源)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">components</code></li> <li data-v-179547fc=""><code data-v-179547fc="">directives</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">组合</strong> (合并 property 至选项内)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">extends</code></li> <li data-v-179547fc=""><code data-v-179547fc="">mixins</code></li> <li data-v-179547fc=""><code data-v-179547fc="">provide</code>/<code data-v-179547fc="">inject</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">接口</strong> (组件的接口)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">inheritAttrs</code></li> <li data-v-179547fc=""><code data-v-179547fc="">props</code></li> <li data-v-179547fc=""><code data-v-179547fc="">emits</code></li> <li data-v-179547fc=""><code data-v-179547fc="">expose</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">组合式 API</strong> (使用组合式 API 的入口点)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">setup</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">本地状态</strong> (本地的响应式 property)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">data</code></li> <li data-v-179547fc=""><code data-v-179547fc="">computed</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">事件</strong> (通过响应式事件触发的回调)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">watch</code></li> <li data-v-179547fc="">生命周期事件 (按照它们被调用的顺序)
<ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">beforeCreate</code></li> <li data-v-179547fc=""><code data-v-179547fc="">created</code></li> <li data-v-179547fc=""><code data-v-179547fc="">beforeMount</code></li> <li data-v-179547fc=""><code data-v-179547fc="">mounted</code></li> <li data-v-179547fc=""><code data-v-179547fc="">beforeUpdate</code></li> <li data-v-179547fc=""><code data-v-179547fc="">updated</code></li> <li data-v-179547fc=""><code data-v-179547fc="">activated</code></li> <li data-v-179547fc=""><code data-v-179547fc="">deactivated</code></li> <li data-v-179547fc=""><code data-v-179547fc="">beforeUnmount</code></li> <li data-v-179547fc=""><code data-v-179547fc="">unmounted</code></li> <li data-v-179547fc=""><code data-v-179547fc="">errorCaptured</code></li> <li data-v-179547fc=""><code data-v-179547fc="">renderTracked</code></li> <li data-v-179547fc=""><code data-v-179547fc="">renderTriggered</code></li></ul></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">非响应式的 property</strong> (不依赖响应性系统的实例 property)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">methods</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">渲染</strong> (组件输出的声明式描述)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">template</code>/<code data-v-179547fc="">render</code></li></ul></li></ol> <h3 id="元素-attribute-的顺序推荐" data-v-179547fc=""><a href="#元素-attribute-的顺序推荐" class="header-anchor" data-v-179547fc="">#</a> 元素 attribute 的顺序<sup data-p="c" data-v-179547fc="">推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">元素 (包括组件) 的 attribute 应该有统一的顺序。</strong></p> <p data-v-179547fc="">这是我们为组件选项推荐的默认顺序。它们被划分为几大类，你能够由此知道新添加的自定义 attribute 和指令应该被放到哪里。</p> <ol data-v-179547fc=""><li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">定义</strong> (提供组件的选项)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">is</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">列表渲染</strong> (创建相同元素的多个变体)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">v-for</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">条件</strong> (元素是否渲染/显示)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">v-if</code></li> <li data-v-179547fc=""><code data-v-179547fc="">v-else-if</code></li> <li data-v-179547fc=""><code data-v-179547fc="">v-else</code></li> <li data-v-179547fc=""><code data-v-179547fc="">v-show</code></li> <li data-v-179547fc=""><code data-v-179547fc="">v-cloak</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">渲染修饰符</strong> (改变元素的渲染方式)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">v-pre</code></li> <li data-v-179547fc=""><code data-v-179547fc="">v-once</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">全局感知</strong> (要求在组件以外被感知)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">id</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">唯一性 Attribute</strong> (需要唯一值的 attribute)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">ref</code></li> <li data-v-179547fc=""><code data-v-179547fc="">key</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">双向绑定</strong> (结合了绑定与事件)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">v-model</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">其他 Attribute</strong> (所有普通的、绑定或未绑定的 attribute)</p></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">事件</strong> (组件事件监听器)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">v-on</code></li></ul></li> <li data-v-179547fc=""><p data-v-179547fc=""><strong data-v-179547fc="">内容</strong> (覆写元素的内容)</p> <ul data-v-179547fc=""><li data-v-179547fc=""><code data-v-179547fc="">v-html</code></li> <li data-v-179547fc=""><code data-v-179547fc="">v-text</code></li></ul></li></ol> <h3 id="组件-实例选项中的空行推荐" data-v-179547fc=""><a href="#组件-实例选项中的空行推荐" class="header-anchor" data-v-179547fc="">#</a> 组件/实例选项中的空行<sup data-p="c" data-v-179547fc="">推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">你可能想在多个 property 之间增加一个空行，特别是在这些选项多到一屏放不下，需要滚动才能看完的时候。</strong></p> <p data-v-179547fc="">当你开始觉得组件密集或难以阅读时，在多个 property 之间添加空行可以使其重新变得易于阅读。在一些诸如 Vim 的编辑器里，被这样格式化后的选项还能通过键盘快速导航。</p> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token literal-property property" data-v-179547fc="">props</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">value</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token literal-property property" data-v-179547fc="">type</span><span class="token operator" data-v-179547fc="">:</span> String<span class="token punctuation" data-v-179547fc="">,</span>
    <span class="token literal-property property" data-v-179547fc="">required</span><span class="token operator" data-v-179547fc="">:</span> <span class="token boolean" data-v-179547fc="">true</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token literal-property property" data-v-179547fc="">focused</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token literal-property property" data-v-179547fc="">type</span><span class="token operator" data-v-179547fc="">:</span> Boolean<span class="token punctuation" data-v-179547fc="">,</span>
    <span class="token keyword" data-v-179547fc="">default</span><span class="token operator" data-v-179547fc="">:</span> <span class="token boolean" data-v-179547fc="">false</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token literal-property property" data-v-179547fc="">label</span><span class="token operator" data-v-179547fc="">:</span> String<span class="token punctuation" data-v-179547fc="">,</span>
  <span class="token literal-property property" data-v-179547fc="">icon</span><span class="token operator" data-v-179547fc="">:</span> String
<span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

<span class="token literal-property property" data-v-179547fc="">computed</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token function" data-v-179547fc="">formattedValue</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token comment" data-v-179547fc="">// ...</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token function" data-v-179547fc="">inputClasses</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token comment" data-v-179547fc="">// ...</span>
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">12</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">13</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">14</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">15</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">16</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">17</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">18</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">19</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">20</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">21</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">22</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">23</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">24</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">// 在组件仍然能够被轻松阅读与定位时，</span>
<span class="token comment" data-v-179547fc="">// 没有空行也挺好</span>
<span class="token literal-property property" data-v-179547fc="">props</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">value</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token literal-property property" data-v-179547fc="">type</span><span class="token operator" data-v-179547fc="">:</span> String<span class="token punctuation" data-v-179547fc="">,</span>
    <span class="token literal-property property" data-v-179547fc="">required</span><span class="token operator" data-v-179547fc="">:</span> <span class="token boolean" data-v-179547fc="">true</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>
  <span class="token literal-property property" data-v-179547fc="">focused</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token literal-property property" data-v-179547fc="">type</span><span class="token operator" data-v-179547fc="">:</span> Boolean<span class="token punctuation" data-v-179547fc="">,</span>
    <span class="token keyword" data-v-179547fc="">default</span><span class="token operator" data-v-179547fc="">:</span> <span class="token boolean" data-v-179547fc="">false</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>
  <span class="token literal-property property" data-v-179547fc="">label</span><span class="token operator" data-v-179547fc="">:</span> String<span class="token punctuation" data-v-179547fc="">,</span>
  <span class="token literal-property property" data-v-179547fc="">icon</span><span class="token operator" data-v-179547fc="">:</span> String
<span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>
<span class="token literal-property property" data-v-179547fc="">computed</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token function" data-v-179547fc="">formattedValue</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token comment" data-v-179547fc="">// ...</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>
  <span class="token function" data-v-179547fc="">inputClasses</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token comment" data-v-179547fc="">// ...</span>
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">12</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">13</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">14</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">15</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">16</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">17</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">18</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">19</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">20</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">21</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">22</span><br data-v-179547fc=""></div></div></div> <h3 id="单文件组件的顶级元素的顺序推荐" data-v-179547fc=""><a href="#单文件组件的顶级元素的顺序推荐" class="header-anchor" data-v-179547fc="">#</a> 单文件组件的顶级元素的顺序<sup data-p="c" data-v-179547fc="">推荐</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc=""><a href="single-file-component.html" data-v-179547fc="">单文件组件</a>应始终保持 <code data-v-179547fc="">&lt;script&gt;</code>、<code data-v-179547fc="">&lt;template&gt;</code> 和 <code data-v-179547fc="">&lt;style&gt;</code> 标签的顺序一致。且 <code data-v-179547fc="">&lt;style&gt;</code> 要放在最后，因为另外两个标签至少会有一个。</strong></p> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token style" data-v-179547fc=""><span class="token language-css" data-v-179547fc=""><span class="token comment" data-v-179547fc="">/* ... */</span></span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token script" data-v-179547fc=""><span class="token language-javascript" data-v-179547fc=""><span class="token comment" data-v-179547fc="">/* ... */</span></span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>...<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">&lt;!-- ComponentA.vue --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token script" data-v-179547fc=""><span class="token language-javascript" data-v-179547fc=""><span class="token comment" data-v-179547fc="">/* ... */</span></span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>...<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token style" data-v-179547fc=""><span class="token language-css" data-v-179547fc=""><span class="token comment" data-v-179547fc="">/* ... */</span></span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>

<span class="token comment" data-v-179547fc="">&lt;!-- ComponentB.vue --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>...<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token script" data-v-179547fc=""><span class="token language-javascript" data-v-179547fc=""><span class="token comment" data-v-179547fc="">/* ... */</span></span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token style" data-v-179547fc=""><span class="token language-css" data-v-179547fc=""><span class="token comment" data-v-179547fc="">/* ... */</span></span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">&lt;!-- ComponentA.vue --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token script" data-v-179547fc=""><span class="token language-javascript" data-v-179547fc=""><span class="token comment" data-v-179547fc="">/* ... */</span></span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>...<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token style" data-v-179547fc=""><span class="token language-css" data-v-179547fc=""><span class="token comment" data-v-179547fc="">/* ... */</span></span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>

<span class="token comment" data-v-179547fc="">&lt;!-- ComponentB.vue --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token script" data-v-179547fc=""><span class="token language-javascript" data-v-179547fc=""><span class="token comment" data-v-179547fc="">/* ... */</span></span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>...<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token style" data-v-179547fc=""><span class="token language-css" data-v-179547fc=""><span class="token comment" data-v-179547fc="">/* ... */</span></span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">&lt;!-- ComponentA.vue --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>...<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token script" data-v-179547fc=""><span class="token language-javascript" data-v-179547fc=""><span class="token comment" data-v-179547fc="">/* ... */</span></span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token style" data-v-179547fc=""><span class="token language-css" data-v-179547fc=""><span class="token comment" data-v-179547fc="">/* ... */</span></span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>

<span class="token comment" data-v-179547fc="">&lt;!-- ComponentB.vue --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>...<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token script" data-v-179547fc=""><span class="token language-javascript" data-v-179547fc=""><span class="token comment" data-v-179547fc="">/* ... */</span></span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token style" data-v-179547fc=""><span class="token language-css" data-v-179547fc=""><span class="token comment" data-v-179547fc="">/* ... */</span></span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""></div></div></div> <h2 id="优先级-d-的规则-谨慎使用-潜在风险" data-v-179547fc=""><a href="#优先级-d-的规则-谨慎使用-潜在风险" class="header-anchor" data-v-179547fc="">#</a> 优先级 D 的规则：谨慎使用 <span class="hide-from-sidebar" data-v-179547fc="">(潜在风险)</span></h2> <h3 id="scoped-中的元素选择器谨慎使用" data-v-179547fc=""><a href="#scoped-中的元素选择器谨慎使用" class="header-anchor" data-v-179547fc="">#</a> <code data-v-179547fc="">scoped</code> 中的元素选择器<sup data-p="d" data-v-179547fc="">谨慎使用</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">元素选择器应该避免在 <code data-v-179547fc="">scoped</code> 中出现。</strong></p> <p data-v-179547fc="">在 <code data-v-179547fc="">scoped</code> 样式中，类选择器要比元素选择器更好，因为大量地使用元素选择器是很慢的。</p> <details class="custom-block details" data-v-179547fc=""><summary data-v-179547fc="">详解</summary> <p data-v-179547fc="">为了给样式设置作用域，Vue 会为元素添加一个独一无二的 attribute，例如 <code data-v-179547fc="">data-v-f3f3eg9</code>。然后选择器将被修改，使得在匹配到的元素中，只有带这个 attribute 的才会真正生效 (比如 <code data-v-179547fc="">button[data-v-f3f3eg9]</code>)。</p> <p data-v-179547fc="">问题在于，大量的元素与 attribute 组合的选择器 (比如 <code data-v-179547fc="">button[data-v-f3f3eg9]</code>) 会比类与 attribute 组合的选择器更慢，因此应该尽可能地选用类选择器。</p></details> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>button</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>×<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>button</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>

<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>style</span> <span class="token attr-name" data-v-179547fc="">scoped</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token style" data-v-179547fc=""><span class="token language-css" data-v-179547fc="">
<span class="token selector" data-v-179547fc="">button</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token property" data-v-179547fc="">background-color</span><span class="token punctuation" data-v-179547fc="">:</span> red<span class="token punctuation" data-v-179547fc="">;</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>button</span> <span class="token attr-name" data-v-179547fc="">class</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>btn btn-close<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>×<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>button</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>

<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>style</span> <span class="token attr-name" data-v-179547fc="">scoped</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token style" data-v-179547fc=""><span class="token language-css" data-v-179547fc="">
<span class="token selector" data-v-179547fc="">.btn-close</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token property" data-v-179547fc="">background-color</span><span class="token punctuation" data-v-179547fc="">:</span> red<span class="token punctuation" data-v-179547fc="">;</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>style</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""></div></div></div> <h3 id="隐性的父子组件通信谨慎使用" data-v-179547fc=""><a href="#隐性的父子组件通信谨慎使用" class="header-anchor" data-v-179547fc="">#</a> 隐性的父子组件通信<sup data-p="d" data-v-179547fc="">谨慎使用</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">应该优先通过 prop 和事件进行父子组件之间的通信，而不是通过 <code data-v-179547fc="">this.$parent</code> 或对 prop 做出变更。</strong></p> <p data-v-179547fc="">一个理想的 Vue 应用是 prop 向下传递，事件向上传递的。遵循这一约定会让你的组件更易于理解。然而，在一些边界情况下，对 prop 的变更或 <code data-v-179547fc="">this.$parent</code> 能够简化两个深度耦合的组件。</p> <p data-v-179547fc="">问题在于，这种做法在很多<em data-v-179547fc="">简单</em>的场景下也可能会更方便。但请当心：不要为了一时方便 (少写代码) 而牺牲简明性 (易于理解的状态流)。</p> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc="">app<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">component</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">'TodoItem'</span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">props</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token literal-property property" data-v-179547fc="">todo</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token literal-property property" data-v-179547fc="">type</span><span class="token operator" data-v-179547fc="">:</span> Object<span class="token punctuation" data-v-179547fc="">,</span>
      <span class="token literal-property property" data-v-179547fc="">required</span><span class="token operator" data-v-179547fc="">:</span> <span class="token boolean" data-v-179547fc="">true</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token literal-property property" data-v-179547fc="">template</span><span class="token operator" data-v-179547fc="">:</span> <span class="token string" data-v-179547fc="">'<span class="language-html" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>input</span> <span class="token directive" data-v-179547fc=""><span class="token identifier directive-name" data-v-179547fc="">v-model</span><span class="token directive-expression" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span><span class="token inline-js language-js" data-v-179547fc="">todo<span class="token punctuation" data-v-179547fc="">.</span>text</span><span class="token punctuation" data-v-179547fc="">&quot;</span></span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span></span>'</span>
<span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">)</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc="">app<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">component</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">'TodoItem'</span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">props</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token literal-property property" data-v-179547fc="">todo</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token literal-property property" data-v-179547fc="">type</span><span class="token operator" data-v-179547fc="">:</span> Object<span class="token punctuation" data-v-179547fc="">,</span>
      <span class="token literal-property property" data-v-179547fc="">required</span><span class="token operator" data-v-179547fc="">:</span> <span class="token boolean" data-v-179547fc="">true</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token literal-property property" data-v-179547fc="">methods</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token function" data-v-179547fc="">removeTodo</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>$parent<span class="token punctuation" data-v-179547fc="">.</span>todos <span class="token operator" data-v-179547fc="">=</span> <span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>$parent<span class="token punctuation" data-v-179547fc="">.</span>todos<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">filter</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token parameter" data-v-179547fc="">todo</span> <span class="token operator" data-v-179547fc="">=&gt;</span> todo<span class="token punctuation" data-v-179547fc="">.</span>id <span class="token operator" data-v-179547fc="">!==</span> vm<span class="token punctuation" data-v-179547fc="">.</span>todo<span class="token punctuation" data-v-179547fc="">.</span>id<span class="token punctuation" data-v-179547fc="">)</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token literal-property property" data-v-179547fc="">template</span><span class="token operator" data-v-179547fc="">:</span> <span class="token template-string" data-v-179547fc=""><span class="token template-punctuation string" data-v-179547fc="">`</span><span class="token string" data-v-179547fc=""><span class="language-html" data-v-179547fc="">
    <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>span</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
      <span class="token interpolation" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">{{</span><span class="token inline-js language-js" data-v-179547fc=""> todo<span class="token punctuation" data-v-179547fc="">.</span>text </span><span class="token punctuation" data-v-179547fc="">}}</span></span>
      <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>button</span> <span class="token directive" data-v-179547fc=""><span class="token punctuation directive-shorthand" data-v-179547fc="">@</span><span class="token directive-modifier" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">c</span><span class="token punctuation" data-v-179547fc="">l</span><span class="token punctuation" data-v-179547fc="">i</span><span class="token punctuation" data-v-179547fc="">c</span><span class="token punctuation" data-v-179547fc="">k</span></span><span class="token directive-expression" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span><span class="token inline-js language-js" data-v-179547fc="">removeTodo</span><span class="token punctuation" data-v-179547fc="">&quot;</span></span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
        ×
      <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>button</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
    <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>span</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  </span></span><span class="token template-punctuation string" data-v-179547fc="">`</span></span>
<span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">)</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">12</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">13</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">14</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">15</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">16</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">17</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">18</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">19</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">20</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">21</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">22</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">23</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc="">app<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">component</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">'TodoItem'</span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">props</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token literal-property property" data-v-179547fc="">todo</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token literal-property property" data-v-179547fc="">type</span><span class="token operator" data-v-179547fc="">:</span> Object<span class="token punctuation" data-v-179547fc="">,</span>
      <span class="token literal-property property" data-v-179547fc="">required</span><span class="token operator" data-v-179547fc="">:</span> <span class="token boolean" data-v-179547fc="">true</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token literal-property property" data-v-179547fc="">emits</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">[</span><span class="token string" data-v-179547fc="">'input'</span><span class="token punctuation" data-v-179547fc="">]</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token literal-property property" data-v-179547fc="">template</span><span class="token operator" data-v-179547fc="">:</span> <span class="token template-string" data-v-179547fc=""><span class="token template-punctuation string" data-v-179547fc="">`</span><span class="token string" data-v-179547fc=""><span class="language-html" data-v-179547fc="">
    <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>input</span>
      <span class="token directive" data-v-179547fc=""><span class="token punctuation directive-shorthand" data-v-179547fc="">:</span><span class="token directive-modifier" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">v</span><span class="token punctuation" data-v-179547fc="">a</span><span class="token punctuation" data-v-179547fc="">l</span><span class="token punctuation" data-v-179547fc="">u</span><span class="token punctuation" data-v-179547fc="">e</span></span><span class="token directive-expression" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span><span class="token inline-js language-js" data-v-179547fc="">todo<span class="token punctuation" data-v-179547fc="">.</span>text</span><span class="token punctuation" data-v-179547fc="">&quot;</span></span></span>
      <span class="token directive" data-v-179547fc=""><span class="token punctuation directive-shorthand" data-v-179547fc="">@</span><span class="token directive-modifier" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">i</span><span class="token punctuation" data-v-179547fc="">n</span><span class="token punctuation" data-v-179547fc="">p</span><span class="token punctuation" data-v-179547fc="">u</span><span class="token punctuation" data-v-179547fc="">t</span></span><span class="token directive-expression" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span><span class="token inline-js language-js" data-v-179547fc=""><span class="token function" data-v-179547fc="">$emit</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">'input'</span><span class="token punctuation" data-v-179547fc="">,</span> $event<span class="token punctuation" data-v-179547fc="">.</span>target<span class="token punctuation" data-v-179547fc="">.</span>value<span class="token punctuation" data-v-179547fc="">)</span></span><span class="token punctuation" data-v-179547fc="">&quot;</span></span></span>
    <span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  </span></span><span class="token template-punctuation string" data-v-179547fc="">`</span></span>
<span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">)</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">12</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">13</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">14</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">15</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">16</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">17</span><br data-v-179547fc=""></div></div><div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc="">app<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">component</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">'TodoItem'</span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">props</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token literal-property property" data-v-179547fc="">todo</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token literal-property property" data-v-179547fc="">type</span><span class="token operator" data-v-179547fc="">:</span> Object<span class="token punctuation" data-v-179547fc="">,</span>
      <span class="token literal-property property" data-v-179547fc="">required</span><span class="token operator" data-v-179547fc="">:</span> <span class="token boolean" data-v-179547fc="">true</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token literal-property property" data-v-179547fc="">emits</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">[</span><span class="token string" data-v-179547fc="">'delete'</span><span class="token punctuation" data-v-179547fc="">]</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token literal-property property" data-v-179547fc="">template</span><span class="token operator" data-v-179547fc="">:</span> <span class="token template-string" data-v-179547fc=""><span class="token template-punctuation string" data-v-179547fc="">`</span><span class="token string" data-v-179547fc=""><span class="language-html" data-v-179547fc="">
    <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>span</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
      <span class="token interpolation" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">{{</span><span class="token inline-js language-js" data-v-179547fc=""> todo<span class="token punctuation" data-v-179547fc="">.</span>text </span><span class="token punctuation" data-v-179547fc="">}}</span></span>
      <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>button</span> <span class="token directive" data-v-179547fc=""><span class="token punctuation directive-shorthand" data-v-179547fc="">@</span><span class="token directive-modifier" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">c</span><span class="token punctuation" data-v-179547fc="">l</span><span class="token punctuation" data-v-179547fc="">i</span><span class="token punctuation" data-v-179547fc="">c</span><span class="token punctuation" data-v-179547fc="">k</span></span><span class="token directive-expression" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span><span class="token inline-js language-js" data-v-179547fc=""><span class="token function" data-v-179547fc="">$emit</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">'delete'</span><span class="token punctuation" data-v-179547fc="">)</span></span><span class="token punctuation" data-v-179547fc="">&quot;</span></span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
        ×
      <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>button</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
    <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>span</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  </span></span><span class="token template-punctuation string" data-v-179547fc="">`</span></span>
<span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">)</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">12</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">13</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">14</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">15</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">16</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">17</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">18</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">19</span><br data-v-179547fc=""></div></div></div> <h3 id="非-flux-的全局状态管理谨慎使用" data-v-179547fc=""><a href="#非-flux-的全局状态管理谨慎使用" class="header-anchor" data-v-179547fc="">#</a> 非 Flux 的全局状态管理<sup data-p="d" data-v-179547fc="">谨慎使用</sup></h3> <p data-v-179547fc=""><strong data-v-179547fc="">应该优先通过 <a href="index5.html" target="_blank" rel="noopener noreferrer" data-v-179547fc="">Vuex<svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound" data-v-179547fc=""><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> 管理全局状态，而不是通过 <code data-v-179547fc="">this.$root</code> 或一个全局事件总线。</strong></p> <p data-v-179547fc="">通过 <code data-v-179547fc="">this.$root</code> 和/或全局事件总线管理状态在很多简单的场景下都是很方便的，但是并不适用于绝大多数的应用。</p> <p data-v-179547fc="">Vuex 是 Vue 的<a href="state-management.html#类-flux-状态管理的官方实现" data-v-179547fc="">官方类 flux 实现</a>，其提供的不仅是一个管理状态的中心，还包括组织、追踪和调试状态变更的工具。它很好地集成在了 Vue 生态系统之中 (包括完整的 <a href="installation.html#vue-devtools" data-v-179547fc="">Vue DevTools</a> 支持)。</p> <div class="style-example style-example-bad" data-v-179547fc=""><h4 data-v-179547fc="">反面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">// main.js</span>
<span class="token keyword" data-v-179547fc="">import</span> <span class="token punctuation" data-v-179547fc="">{</span> createApp <span class="token punctuation" data-v-179547fc="">}</span> <span class="token keyword" data-v-179547fc="">from</span> <span class="token string" data-v-179547fc="">'vue'</span>
<span class="token keyword" data-v-179547fc="">import</span> mitt <span class="token keyword" data-v-179547fc="">from</span> <span class="token string" data-v-179547fc="">'mitt'</span>
<span class="token keyword" data-v-179547fc="">const</span> app <span class="token operator" data-v-179547fc="">=</span> <span class="token function" data-v-179547fc="">createApp</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token function" data-v-179547fc="">data</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token keyword" data-v-179547fc="">return</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token literal-property property" data-v-179547fc="">todos</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">[</span><span class="token punctuation" data-v-179547fc="">]</span><span class="token punctuation" data-v-179547fc="">,</span>
      <span class="token literal-property property" data-v-179547fc="">emitter</span><span class="token operator" data-v-179547fc="">:</span> <span class="token function" data-v-179547fc="">mitt</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token function" data-v-179547fc="">created</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>emitter<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">on</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">'remove-todo'</span><span class="token punctuation" data-v-179547fc="">,</span> <span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>removeTodo<span class="token punctuation" data-v-179547fc="">)</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token literal-property property" data-v-179547fc="">methods</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token function" data-v-179547fc="">removeTodo</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token parameter" data-v-179547fc="">todo</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token keyword" data-v-179547fc="">const</span> todoIdToRemove <span class="token operator" data-v-179547fc="">=</span> todo<span class="token punctuation" data-v-179547fc="">.</span>id
      <span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>todos <span class="token operator" data-v-179547fc="">=</span> <span class="token keyword" data-v-179547fc="">this</span><span class="token punctuation" data-v-179547fc="">.</span>todos<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">filter</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token parameter" data-v-179547fc="">todo</span> <span class="token operator" data-v-179547fc="">=&gt;</span> todo<span class="token punctuation" data-v-179547fc="">.</span>id <span class="token operator" data-v-179547fc="">!==</span> todoIdToRemove<span class="token punctuation" data-v-179547fc="">)</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">)</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">12</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">13</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">14</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">15</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">16</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">17</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">18</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">19</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">20</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">21</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">22</span><br data-v-179547fc=""></div></div></div> <div class="style-example style-example-good" data-v-179547fc=""><h4 data-v-179547fc="">正面例子</h4> <div class="language-js line-numbers-mode" data-v-179547fc=""><pre class="language-js" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">// store/modules/todos.js</span>
<span class="token keyword" data-v-179547fc="">export</span> <span class="token keyword" data-v-179547fc="">default</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">state</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token literal-property property" data-v-179547fc="">list</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">[</span><span class="token punctuation" data-v-179547fc="">]</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token literal-property property" data-v-179547fc="">mutations</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token constant" data-v-179547fc="">REMOVE_TODO</span> <span class="token punctuation" data-v-179547fc="">(</span><span class="token parameter" data-v-179547fc="">state<span class="token punctuation" data-v-179547fc="">,</span> todoId</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
      state<span class="token punctuation" data-v-179547fc="">.</span>list <span class="token operator" data-v-179547fc="">=</span> state<span class="token punctuation" data-v-179547fc="">.</span>list<span class="token punctuation" data-v-179547fc="">.</span><span class="token function" data-v-179547fc="">filter</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token parameter" data-v-179547fc="">todo</span> <span class="token operator" data-v-179547fc="">=&gt;</span> todo<span class="token punctuation" data-v-179547fc="">.</span>id <span class="token operator" data-v-179547fc="">!==</span> todoId<span class="token punctuation" data-v-179547fc="">)</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token literal-property property" data-v-179547fc="">actions</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token function" data-v-179547fc="">removeTodo</span> <span class="token punctuation" data-v-179547fc="">(</span><span class="token parameter" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">{</span> commit<span class="token punctuation" data-v-179547fc="">,</span> state <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span> todo</span><span class="token punctuation" data-v-179547fc="">)</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token function" data-v-179547fc="">commit</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token string" data-v-179547fc="">'REMOVE_TODO'</span><span class="token punctuation" data-v-179547fc="">,</span> todo<span class="token punctuation" data-v-179547fc="">.</span>id<span class="token punctuation" data-v-179547fc="">)</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">12</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">13</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">14</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">15</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">16</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">17</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">18</span><br data-v-179547fc=""></div></div><div class="language-html line-numbers-mode" data-v-179547fc=""><pre class="language-html" data-v-179547fc=""><code data-v-179547fc=""><span class="token comment" data-v-179547fc="">&lt;!-- TodoItem.vue --&gt;</span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>span</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
    {{ todo.text }}
    <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>button</span> <span class="token attr-name" data-v-179547fc="">@click</span><span class="token attr-value" data-v-179547fc=""><span class="token punctuation attr-equals" data-v-179547fc="">=</span><span class="token punctuation" data-v-179547fc="">&quot;</span>removeTodo(todo)<span class="token punctuation" data-v-179547fc="">&quot;</span></span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
      X
    <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>button</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
  <span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>span</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>template</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>

<span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span><span class="token script" data-v-179547fc=""><span class="token language-javascript" data-v-179547fc="">
<span class="token keyword" data-v-179547fc="">import</span> <span class="token punctuation" data-v-179547fc="">{</span> mapActions <span class="token punctuation" data-v-179547fc="">}</span> <span class="token keyword" data-v-179547fc="">from</span> <span class="token string" data-v-179547fc="">'vuex'</span>

<span class="token keyword" data-v-179547fc="">export</span> <span class="token keyword" data-v-179547fc="">default</span> <span class="token punctuation" data-v-179547fc="">{</span>
  <span class="token literal-property property" data-v-179547fc="">props</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
    <span class="token literal-property property" data-v-179547fc="">todo</span><span class="token operator" data-v-179547fc="">:</span> <span class="token punctuation" data-v-179547fc="">{</span>
      <span class="token literal-property property" data-v-179547fc="">type</span><span class="token operator" data-v-179547fc="">:</span> Object<span class="token punctuation" data-v-179547fc="">,</span>
      <span class="token literal-property property" data-v-179547fc="">required</span><span class="token operator" data-v-179547fc="">:</span> <span class="token boolean" data-v-179547fc="">true</span>
    <span class="token punctuation" data-v-179547fc="">}</span>
  <span class="token punctuation" data-v-179547fc="">}</span><span class="token punctuation" data-v-179547fc="">,</span>

  <span class="token literal-property property" data-v-179547fc="">methods</span><span class="token operator" data-v-179547fc="">:</span> <span class="token function" data-v-179547fc="">mapActions</span><span class="token punctuation" data-v-179547fc="">(</span><span class="token punctuation" data-v-179547fc="">[</span><span class="token string" data-v-179547fc="">'removeTodo'</span><span class="token punctuation" data-v-179547fc="">]</span><span class="token punctuation" data-v-179547fc="">)</span>
<span class="token punctuation" data-v-179547fc="">}</span>
</span></span><span class="token tag" data-v-179547fc=""><span class="token tag" data-v-179547fc=""><span class="token punctuation" data-v-179547fc="">&lt;/</span>script</span><span class="token punctuation" data-v-179547fc="">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper" data-v-179547fc=""><span class="line-number" data-v-179547fc="">1</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">2</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">3</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">4</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">5</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">6</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">7</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">8</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">9</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">10</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">11</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">12</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">13</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">14</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">15</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">16</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">17</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">18</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">19</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">20</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">21</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">22</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">23</span><br data-v-179547fc=""><span class="line-number" data-v-179547fc="">24</span><br data-v-179547fc=""></div></div></div></div> <footer class="page-edit" data-v-007274aa=""><div class="container" data-v-007274aa=""><p data-v-007274aa=""><span class="edit-link" data-v-007274aa="">
         发现了错误或者想要为文档做贡献？
          <a href="javascript:;" target="_blank" rel="noopener noreferrer" data-v-007274aa="">
            在 GitHub 上编辑此页
            <svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="outbound-link-title" role="img" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound" data-v-007274aa=""><title id="outbound-link-title">(opens new window)</title> <path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></span> <br data-v-007274aa=""> <span class="prefix" data-v-007274aa="">最近更新时间:</span> <span class="time" data-v-007274aa="">2021-11-25, 15:00:08 UTC</span></p></div></footer> <!----> <div class="bsa-cpc-wrapper"><div class="bsa-cpc"></div></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="static/js/app.824bcde7.js" defer=""></script><script src="static/js/11.8d95dd7b.js" defer=""></script><script src="static/js/34.1f3cec39.js" defer=""></script><script src="static/js/22.4fdda504.js" defer=""></script>
  </body>
</html>
